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_macDefaultEncodingIsPC
= true ;
108 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
109 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
110 long wxApp::s_macPreferencesMenuItemId
= wxID_PREFERENCES
;
111 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
112 wxString
wxApp::s_macHelpMenuTitleName
= wxT("&Help") ;
114 //----------------------------------------------------------------------
115 // Core Apple Event Support
116 //----------------------------------------------------------------------
118 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
119 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
120 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
121 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
123 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
125 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
128 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
130 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
133 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
135 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
138 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
140 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
143 // AEODoc Calls MacOpenFile on each of the files passed
145 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
149 DescType returnedType
;
155 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
159 err
= AECountItems(&docList
, &itemsInList
);
163 ProcessSerialNumber PSN
;
164 PSN
.highLongOfPSN
= 0 ;
165 PSN
.lowLongOfPSN
= kCurrentProcess
;
166 SetFrontProcess( &PSN
) ;
168 for (i
= 1; i
<= itemsInList
; i
++) {
169 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
170 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
171 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
177 // AEPDoc Calls MacPrintFile on each of the files passed
179 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
183 DescType returnedType
;
189 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
193 err
= AECountItems(&docList
, &itemsInList
);
197 ProcessSerialNumber PSN
;
198 PSN
.highLongOfPSN
= 0 ;
199 PSN
.lowLongOfPSN
= kCurrentProcess
;
200 SetFrontProcess( &PSN
) ;
202 for (i
= 1; i
<= itemsInList
; i
++) {
203 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
204 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
205 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
211 // AEOApp calls MacNewFile
213 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
219 // AEQuit attempts to quit the application
221 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
223 /* wxWindow* win = GetTopWindow() ;
236 //----------------------------------------------------------------------
237 // Support Routines linking the Mac...File Calls to the Document Manager
238 //----------------------------------------------------------------------
240 void wxApp::MacOpenFile(const wxString
& fileName
)
242 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
244 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
247 void wxApp::MacPrintFile(const wxString
& fileName
)
249 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
252 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
255 wxView
* view
= doc
->GetFirstView() ;
258 wxPrintout
*printout
= view
->OnCreatePrintout();
262 printer
.Print(view
->GetFrame(), printout
, TRUE
);
268 doc
->DeleteAllViews();
269 dm
->RemoveDocument(doc
) ;
275 void wxApp::MacNewFile()
279 //----------------------------------------------------------------------
280 // Carbon Event Handler
281 //----------------------------------------------------------------------
285 static const EventTypeSpec eventList
[] =
287 { kEventClassCommand
, kEventProcessCommand
} ,
288 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
290 { kEventClassMenu
, kEventMenuOpening
},
291 { kEventClassMenu
, kEventMenuClosed
},
293 { kEventClassApplication
, kEventAppActivated
} ,
294 { kEventClassApplication
, kEventAppDeactivated
} ,
295 // handling the quit event is not recommended by apple
296 // rather using the quit apple event - which we do
298 { kEventClassAppleEvent
, kEventAppleEvent
} ,
300 { kEventClassMouse
, kEventMouseDown
} ,
301 { kEventClassMouse
, kEventMouseMoved
} ,
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 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
342 OSStatus result
= eventNotHandledErr
;
345 UInt32 modifiers
= 0;
346 EventMouseButton button
= 0 ;
349 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
350 sizeof( Point
), NULL
, &point
);
351 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
,
352 sizeof( UInt32
), NULL
, &modifiers
);
353 GetEventParameter( event
, kEventParamMouseButton
, typeMouseButton
, NULL
,
354 sizeof( EventMouseButton
), NULL
, &button
);
355 GetEventParameter( event
, kEventParamClickCount
, typeUInt32
, NULL
,
356 sizeof( UInt32
), NULL
, &click
);
358 if ( button
== 0 || GetEventKind( event
) == kEventMouseUp
)
359 modifiers
+= btnState
;
362 switch( GetEventKind(event
) )
364 case kEventMouseDown
:
368 short windowPart
= ::FindWindow(point
, &window
);
370 if ( windowPart
== inMenuBar
)
372 MenuSelect( point
) ;
377 case kEventMouseMoved
:
379 wxTheApp
->MacHandleMouseMovedEvent( point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
389 static pascal OSStatus
CommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
391 OSStatus result
= eventNotHandledErr
;
395 GetEventParameter( event
, kEventParamDirectObject
, typeHICommand
, NULL
,
396 sizeof( HICommand
), NULL
, &command
);
398 MenuCommand id
= command
.commandID
;
399 if ( id
== kHICommandPreferences
)
400 id
= wxApp::s_macPreferencesMenuItemId
;
402 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
403 wxMenu
* menu
= NULL
;
404 wxMenuItem
* item
= NULL
;
407 item
= mbar
->FindItem( id
, &menu
) ;
409 if ( item
== NULL
|| menu
== NULL
|| mbar
== NULL
)
412 switch( GetEventKind( event
) )
414 case kEventProcessCommand
:
416 if (item
->IsCheckable())
418 item
->Check( !item
->IsChecked() ) ;
421 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
425 case kEventCommandUpdateStatus
:
426 // eventually trigger an updateui round
436 static pascal OSStatus
ApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
438 OSStatus result
= eventNotHandledErr
;
439 switch ( GetEventKind( event
) )
441 case kEventAppActivated
:
444 wxTheApp
->MacResume( true ) ;
448 case kEventAppDeactivated
:
451 wxTheApp
->MacSuspend( true ) ;
461 pascal OSStatus
wxAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
463 OSStatus result
= eventNotHandledErr
;
464 switch( GetEventClass( event
) )
466 case kEventClassCommand
:
467 result
= CommandEventHandler( handler
, event
, data
) ;
469 case kEventClassApplication
:
470 result
= ApplicationEventHandler( handler
, event
, data
) ;
472 case kEventClassMenu
:
473 result
= MenuEventHandler( handler
, event
, data
) ;
475 case kEventClassMouse
:
476 result
= MouseEventHandler( handler
, event
, data
) ;
478 case kEventClassAppleEvent
:
481 wxMacConvertEventToRecord( event
, &rec
) ;
482 result
= AEProcessAppleEvent( &rec
) ;
492 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler
)
496 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
497 // we know it's there ;-)
498 WXIMPORT
char std::__throws_bad_alloc
;
501 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
507 UMAInitToolbox( 4 ) ;
508 SetEventMask( everyEvent
) ;
509 UMAShowWatchCursor() ;
511 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
512 // open shared library resources from here since we don't have
513 // __wxinitialize in Mach-O shared libraries
514 wxStAppResource::OpenSharedLibraryResource(NULL
);
518 // test the minimal configuration necessary
524 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
526 error
= kMacSTRWrongMachine
;
528 else if (theMachine
< gestaltMacPlus
)
530 error
= kMacSTRWrongMachine
;
532 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
534 error
= kMacSTROldSystem
;
536 else if ( theSystem
< 0x0860 )
538 error
= kMacSTROldSystem
;
540 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
542 error
= kMacSTRSmallSize
;
548 if ( !UMAHasAppearance() )
550 error = kMacSTRNoPre8Yet ;
556 // if we encountered any problems so far, give the error code and exit immediately
560 wxStAppResource resload
;
564 GetIndString(message
, 128, error
);
565 UMAShowArrowCursor() ;
566 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
567 itemHit
= Alert(128, nil
);
572 # if __option(profile)
573 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
578 // now avoid exceptions thrown for new (bad_alloc)
579 // FIXME CS for some changes outside wxMac does not compile anymore
581 std::__throws_bad_alloc
= 0 ;
585 wxMacSetupConverters() ;
587 s_macCursorRgn
= ::NewRgn() ;
589 // Mac OS X passes a process serial number command line argument when
590 // the application is launched from the Finder. This argument must be
591 // removed from the command line arguments before being handled by the
592 // application (otherwise applications would need to handle it)
595 static const wxChar
*ARG_PSN
= _T("-psn_");
596 if ( wxStrncmp(argv
[1], ARG_PSN
, sizeof(ARG_PSN
) - 1) == 0 )
598 // remove this argument
599 memmove(argv
, argv
+ 1, argc
--);
603 if ( !wxAppBase::Initialize(argc
, argv
) )
606 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
607 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
609 wxMacCreateNotifierTable() ;
611 UMAShowArrowCursor() ;
616 bool wxApp::OnInitGui()
618 if( !wxAppBase::OnInitGui() )
622 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
624 InstallApplicationEventHandler(
625 GetwxAppEventHandlerUPP(),
626 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
629 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
630 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
631 NewAEEventHandlerUPP(AEHandleODoc
) ,
633 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
634 NewAEEventHandlerUPP(AEHandleOApp
) ,
636 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
637 NewAEEventHandlerUPP(AEHandlePDoc
) ,
639 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
640 NewAEEventHandlerUPP(AEHandleQuit
) ,
643 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
644 NewAEEventHandlerProc(AEHandleODoc
) ,
646 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
647 NewAEEventHandlerProc(AEHandleOApp
) ,
649 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
650 NewAEEventHandlerProc(AEHandlePDoc
) ,
652 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
653 NewAEEventHandlerProc(AEHandleQuit
) ,
660 void wxApp::CleanUp()
662 wxToolTip::RemoveToolTips() ;
664 // One last chance for pending objects to be cleaned up
665 wxTheApp
->DeletePendingObjects();
667 wxMacDestroyNotifierTable() ;
669 delete wxWinMacWindowList
;
670 wxWinMacWindowList
= NULL
;
672 delete wxWinMacControlList
;
673 wxWinMacControlList
= NULL
;
676 # if __option(profile)
677 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
682 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
683 // close shared library resources from here since we don't have
684 // __wxterminate in Mach-O shared libraries
685 wxStAppResource::CloseSharedLibraryResource();
687 wxMacCleanupConverters() ;
689 UMACleanupToolbox() ;
690 if (s_macCursorRgn
) {
691 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
698 wxAppBase::CleanUp();
701 //----------------------------------------------------------------------
702 // misc initialization stuff
703 //----------------------------------------------------------------------
705 // extern variable for shared library resource id
706 // need to be able to find it with NSLookupAndBindSymbol
707 short gSharedLibraryResource
= kResFileNotOpened
;
709 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
710 CFBundleRef gSharedLibraryBundle
= NULL
;
711 #endif /* WXMAKINGDLL_CORE && __DARWIN__ */
713 wxStAppResource::wxStAppResource()
715 m_currentRefNum
= CurResFile() ;
716 if ( gSharedLibraryResource
!= kResFileNotOpened
)
718 UseResFile( gSharedLibraryResource
) ;
722 wxStAppResource::~wxStAppResource()
724 if ( m_currentRefNum
!= kResFileNotOpened
)
726 UseResFile( m_currentRefNum
) ;
730 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
732 gSharedLibraryResource
= kResFileNotOpened
;
734 #ifdef WXMAKINGDLL_CORE
735 if ( initBlock
!= NULL
) {
736 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
737 FSSpec
*fileSpec
= NULL
;
739 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
740 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
742 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
743 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
746 if (fileSpec
!= NULL
) {
747 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
752 // Open the shared library resource file if it is not yet open
755 const char *theLibPath
;
757 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
758 if (gSharedLibraryBundle
!= NULL
) {
759 // wxWindows has been bundled into a framework
760 // load the framework resources
762 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
765 // wxWindows is a simple dynamic shared library
766 // load the resources from the data fork of a separate resource file
770 OSErr theErr
= noErr
;
772 // get the library path
773 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
774 theModule
= NSModuleForSymbol(theSymbol
);
775 theLibPath
= NSLibraryNameForModule(theModule
);
777 // if we call wxLogDebug from here then, as wxTheApp hasn't been
778 // created yet when we're called from wxApp::Initialize(), wxLog
779 // is going to create a default stderr-based log target instead of
780 // the expected normal GUI one -- don't do it, if we really want
781 // to see this message just use fprintf() here
783 wxLogDebug( wxT("wxMac library installation name is '%s'"),
787 // allocate copy to replace .dylib.* extension with .rsrc
788 if (theLibPath
!= NULL
) {
789 theResPath
= theLibPath
;
790 // replace '_core' with '' in case of multi-lib build
791 theResPath
.Replace(wxT("_core"), wxEmptyString
);
792 // replace ".dylib" shared library extension with ".rsrc"
793 theResPath
.Replace(wxT(".dylib"), wxT(".rsrc"));
794 // Find the begining of the filename
795 theName
= theResPath
.AfterLast('/');
798 wxLogDebug( wxT("wxMac resources file name is '%s'"),
799 theResPath
.mb_str() );
802 theErr
= FSPathMakeRef((UInt8
*) theResPath
.mb_str(), &theResRef
, false);
803 if (theErr
!= noErr
) {
804 // try in current directory (using name only)
805 theErr
= FSPathMakeRef((UInt8
*) theName
.mb_str(), &theResRef
, false);
808 // open the resource file
809 if (theErr
== noErr
) {
810 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
811 &gSharedLibraryResource
);
813 if (theErr
!= noErr
) {
816 wxT("unable to open wxMac resource file '%s'\n"),
817 theResPath
.mb_str() );
818 #endif // __WXDEBUG__
823 #endif /* __DARWIN__ */
825 #endif /* WXMAKINGDLL_CORE */
828 void wxStAppResource::CloseSharedLibraryResource()
830 #ifdef WXMAKINGDLL_CORE
831 // Close the shared library resource file
832 if (gSharedLibraryResource
!= kResFileNotOpened
) {
834 if (gSharedLibraryBundle
!= NULL
) {
835 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
836 gSharedLibraryResource
);
837 gSharedLibraryBundle
= NULL
;
840 #endif /* __DARWIN__ */
842 CloseResFile(gSharedLibraryResource
);
844 gSharedLibraryResource
= kResFileNotOpened
;
846 #endif /* WXMAKINGDLL_CORE */
849 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
851 // for shared libraries we have to manually get the correct resource
852 // ref num upon initializing and releasing when terminating, therefore
853 // the __wxinitialize and __wxterminate must be used
856 void __sinit(void); /* (generated by linker) */
857 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
858 pascal void __terminate(void);
861 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
863 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
864 return __initialize( theInitBlock
) ;
867 pascal void __wxterminate(void)
869 wxStAppResource::CloseSharedLibraryResource() ;
873 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
877 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
879 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
880 OSStatus err
= noErr
;
883 switch( GetEventClass( event
) )
885 case kEventClassKeyboard
:
888 switch( GetEventKind(event
) )
890 case kEventRawKeyDown
:
891 rec
->what
= keyDown
;
893 case kEventRawKeyRepeat
:
894 rec
->what
= autoKey
;
896 case kEventRawKeyUp
:
899 case kEventRawKeyModifiersChanged
:
900 rec
->what
= nullEvent
;
909 unsigned char charCode
;
911 GetMouse( &rec
->where
) ;
913 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
914 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
915 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
916 rec
->modifiers
= modifiers
;
917 rec
->message
= (keyCode
<< 8 ) + charCode
;
921 case kEventClassTextInput
:
923 switch( GetEventKind( event
) )
925 case kEventTextInputUnicodeForKeyEvent
:
928 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
932 unsigned char charCode
;
934 GetMouse( &rec
->where
) ;
935 rec
->what
= keyDown
;
936 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
937 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
938 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
939 rec
->modifiers
= modifiers
;
940 rec
->message
= (keyCode
<< 8 ) + charCode
;
956 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
958 OSStatus result = eventNotHandledErr ;
961 switch ( GetEventClass( event ) )
963 case kEventClassKeyboard :
964 if ( wxMacConvertEventToRecord( event , &rec ) )
966 wxTheApp->MacHandleModifierEvents( &rec ) ;
967 wxTheApp->MacHandleOneEvent( &rec ) ;
971 case kEventClassTextInput :
972 if ( wxMacConvertEventToRecord( event , &rec ) )
974 wxTheApp->MacHandleModifierEvents( &rec ) ;
975 wxTheApp->MacHandleOneEvent( &rec ) ;
989 m_printMode
= wxPRINT_WINDOWS
;
992 m_macCurrentEvent
= NULL
;
993 m_macCurrentEventHandlerCallRef
= NULL
;
996 bool wxApp::Initialized()
1004 int wxApp::MainLoop()
1016 void wxApp::ExitMainLoop()
1018 m_keepGoing
= FALSE
;
1021 // Is a message/event pending?
1022 bool wxApp::Pending()
1025 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1029 return EventAvail( everyEvent
, &event
) ;
1033 // Dispatch a message.
1034 void wxApp::Dispatch()
1039 void wxApp::OnIdle(wxIdleEvent
& event
)
1041 wxAppBase::OnIdle(event
);
1043 // If they are pending events, we must process them: pending events are
1044 // either events to the threads other than main or events posted with
1045 // wxPostEvent() functions
1046 wxMacProcessNotifierAndPendingEvents();
1048 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1049 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1052 void wxApp::WakeUpIdle()
1063 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1066 GetTopWindow()->Close(TRUE
);
1069 // Default behaviour: close the application with prompts. The
1070 // user can veto the close, and therefore the end session.
1071 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1075 if (!GetTopWindow()->Close(!event
.CanVeto()))
1080 extern "C" void wxCYield() ;
1086 // Yield to other processes
1088 bool wxApp::Yield(bool onlyIfNeeded
)
1092 if ( !onlyIfNeeded
)
1094 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1103 YieldToAnyThread() ;
1105 // by definition yield should handle all non-processed events
1109 OSStatus status
= noErr
;
1112 s_inReceiveEvent
= true ;
1113 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1114 s_inReceiveEvent
= false ;
1116 if ( status
== eventLoopTimedOutErr
)
1118 // make sure next time the event loop will trigger idle events
1119 sleepTime
= kEventDurationNoWait
;
1121 else if ( status
== eventLoopQuitErr
)
1123 // according to QA1061 this may also occur when a WakeUp Process
1128 MacHandleOneEvent( theEvent
) ;
1129 ReleaseEvent(theEvent
);
1131 } while( status
== noErr
) ;
1135 // having a larger value here leads to large performance slowdowns
1136 // so we cannot give background apps more processor time here
1137 // we do so however having a large sleep value in the main event loop
1140 while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1142 MacHandleModifierEvents( &event
) ;
1143 MacHandleOneEvent( &event
);
1144 if ( event
.what
!= kHighLevelEvent
)
1145 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1147 MacHandleModifierEvents( &event
) ;
1150 wxMacProcessNotifierAndPendingEvents() ;
1156 // platform specifics
1158 void wxApp::MacSuspend( bool convertClipboard
)
1161 // we have to deactive the top level windows manually
1163 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1166 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1168 #if 0 // having problems right now with that
1169 if (!win
->HasFlag(wxSTAY_ON_TOP
))
1172 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1174 node
= node
->GetNext();
1177 ::HideFloatingWindows() ;
1179 s_lastMouseDown
= 0 ;
1181 if( convertClipboard
)
1183 MacConvertPrivateToPublicScrap() ;
1187 extern wxList wxModalDialogs
;
1189 void wxApp::MacResume( bool convertClipboard
)
1191 s_lastMouseDown
= 0 ;
1192 if( convertClipboard
)
1194 MacConvertPublicToPrivateScrap() ;
1198 ::ShowFloatingWindows() ;
1199 // raise modal dialogs in case a non modal window was selected to activate the app
1201 wxNode
* node
= wxModalDialogs
.GetFirst();
1204 wxDialog
* dialog
= (wxDialog
*) node
->GetData();
1207 node
= node
->GetNext();
1212 void wxApp::MacConvertPrivateToPublicScrap()
1216 void wxApp::MacConvertPublicToPrivateScrap()
1220 void wxApp::MacDoOneEvent()
1225 s_inReceiveEvent
= true ;
1226 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1227 s_inReceiveEvent
= false ;
1228 if ( status
== eventLoopTimedOutErr
)
1230 if ( wxTheApp
->ProcessIdle() )
1231 sleepTime
= kEventDurationNoWait
;
1233 sleepTime
= kEventDurationForever
;
1235 else if ( status
== eventLoopQuitErr
)
1237 // according to QA1061 this may also occur when a WakeUp Process
1242 MacHandleOneEvent( theEvent
) ;
1243 ReleaseEvent(theEvent
);
1244 sleepTime
= kEventDurationNoWait
;
1249 EventMask eventMask
= everyEvent
;
1251 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1253 MacHandleModifierEvents( &event
) ;
1254 MacHandleOneEvent( &event
);
1258 MacHandleModifierEvents( &event
) ;
1260 WindowPtr window
= ::FrontWindow() ;
1262 ::IdleControls( window
) ;
1264 if ( wxTheApp
->ProcessIdle() )
1267 sleepTime
= GetCaretTime() / 2 ;
1269 if ( event
.what
!= kHighLevelEvent
)
1270 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1274 DeletePendingObjects() ;
1275 wxMacProcessNotifierAndPendingEvents() ;
1278 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1281 EventTargetRef theTarget
;
1282 theTarget
= GetEventDispatcherTarget();
1283 m_macCurrentEvent
= evr
;
1284 SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1286 EventRecord
* ev
= (EventRecord
*) evr
;
1287 m_macCurrentEvent
= ev
;
1289 wxApp::sm_lastMessageTime
= ev
->when
;
1294 MacHandleMouseDownEvent( ev
) ;
1295 if ( ev
->modifiers
& controlKey
)
1296 s_lastMouseDown
= 2;
1298 s_lastMouseDown
= 1;
1301 if ( s_lastMouseDown
== 2 )
1303 ev
->modifiers
|= controlKey
;
1307 ev
->modifiers
&= ~controlKey
;
1309 MacHandleMouseUpEvent( ev
) ;
1310 s_lastMouseDown
= 0;
1313 MacHandleActivateEvent( ev
) ;
1316 MacHandleUpdateEvent( ev
) ;
1320 MacHandleKeyDownEvent( ev
) ;
1323 MacHandleKeyUpEvent( ev
) ;
1326 MacHandleDiskEvent( ev
) ;
1329 MacHandleOSEvent( ev
) ;
1331 case kHighLevelEvent
:
1332 MacHandleHighLevelEvent( ev
) ;
1338 wxMacProcessNotifierAndPendingEvents() ;
1342 bool s_macIsInModalLoop
= false ;
1344 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1346 EventRecord
* ev
= (EventRecord
*) evr
;
1347 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1349 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1351 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1352 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1353 event
.m_altDown
= ev
->modifiers
& optionKey
;
1354 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1356 event
.m_x
= ev
->where
.h
;
1357 event
.m_y
= ev
->where
.v
;
1358 event
.m_timeStamp
= ev
->when
;
1359 wxWindow
* focus
= wxWindow::FindFocus() ;
1360 event
.SetEventObject(focus
);
1362 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1364 event
.m_keyCode
= WXK_CONTROL
;
1365 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1366 focus
->GetEventHandler()->ProcessEvent( event
) ;
1368 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1370 event
.m_keyCode
= WXK_SHIFT
;
1371 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1372 focus
->GetEventHandler()->ProcessEvent( event
) ;
1374 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1376 event
.m_keyCode
= WXK_ALT
;
1377 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1378 focus
->GetEventHandler()->ProcessEvent( event
) ;
1380 if ( ev
->modifiers
^ s_lastModifiers
) & cmdKey
)
1382 event
.m_keyCode
= WXK_COMMAND
;
1383 event
.SetEventType( ( ev
->modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1384 focus
->GetEventHandler()->ProcessEvent( event
) ;
1386 s_lastModifiers
= ev
->modifiers
;
1390 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1392 // we must avoid reentrancy problems when processing high level events eg printing
1393 bool former
= s_inYield
;
1395 EventRecord
* ev
= (EventRecord
*) evr
;
1396 ::AEProcessAppleEvent( ev
) ;
1397 s_inYield
= former
;
1400 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1402 EventRecord
* ev
= (EventRecord
*) evr
;
1403 wxToolTip::RemoveToolTips() ;
1406 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1407 WindowAttributes frontWindowAttributes
= NULL
;
1409 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1411 short windowPart
= ::FindWindow(ev
->where
, &window
);
1412 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1413 if ( wxPendingDelete
.Member(win
) )
1417 GetQDGlobalsScreenBits( &screenBits
);
1422 if ( s_macIsInModalLoop
)
1428 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1429 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1430 s_lastMouseDown
= 0;
1434 SystemClick( ev
, window
) ;
1435 s_lastMouseDown
= 0;
1438 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1444 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1449 Point pt
= { 0, 0 } ;
1450 SetPortWindowPort(window
) ;
1451 LocalToGlobal( &pt
) ;
1453 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1454 -1 , wxSIZE_USE_EXISTING
);
1456 s_lastMouseDown
= 0;
1460 if (TrackGoAway(window
, ev
->where
))
1465 s_lastMouseDown
= 0;
1469 Rect newContentRect
;
1470 Rect constraintRect
;
1471 constraintRect
.top
= win
->GetMinHeight() ;
1472 if ( constraintRect
.top
== -1 )
1473 constraintRect
.top
= 0 ;
1474 constraintRect
.left
= win
->GetMinWidth() ;
1475 if ( constraintRect
.left
== -1 )
1476 constraintRect
.left
= 0 ;
1477 constraintRect
.right
= win
->GetMaxWidth() ;
1478 if ( constraintRect
.right
== -1 )
1479 constraintRect
.right
= 32000 ;
1480 constraintRect
.bottom
= win
->GetMaxHeight() ;
1481 if ( constraintRect
.bottom
== -1 )
1482 constraintRect
.bottom
= 32000 ;
1484 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1485 &constraintRect
, &newContentRect
) ;
1488 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1489 newContentRect
.right
- newContentRect
.left
,
1490 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1492 s_lastMouseDown
= 0;
1497 if (TrackBox(window
, ev
->where
, windowPart
))
1499 // TODO setup size event
1500 ZoomWindow( window
, windowPart
, false ) ;
1506 Point pt
= { 0, 0 } ;
1507 SetPortWindowPort(window
) ;
1508 LocalToGlobal( &pt
) ;
1511 GetWindowPortBounds(window
, &tempRect
) ;
1512 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1513 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1516 s_lastMouseDown
= 0;
1518 case inCollapseBox
:
1519 // TODO setup size event
1520 s_lastMouseDown
= 0;
1527 SetPortWindowPort(window
) ;
1530 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1532 if ( s_macIsInModalLoop
)
1536 else if ( UMAIsWindowFloating( window
) )
1539 win
->MacMouseDown( ev
, windowPart
) ;
1543 // Activate window first
1544 ::SelectWindow( window
) ;
1548 win
->MacMouseDown( ev
, windowPart
) ;
1554 win
->MacMouseDown( ev
, windowPart
) ;
1562 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1564 EventRecord
* ev
= (EventRecord
*) evr
;
1567 short windowPart
= inNoWindow
;
1568 if ( wxTheApp
->s_captureWindow
)
1570 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1571 windowPart
= inContent
;
1575 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1586 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1588 win
->MacMouseUp( ev
, windowPart
) ;
1596 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1597 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1602 case kHomeCharCode
:
1605 case kEnterCharCode
:
1606 retval
= WXK_RETURN
;
1611 case kHelpCharCode
:
1614 case kBackspaceCharCode
:
1620 case kPageUpCharCode
:
1621 retval
= WXK_PAGEUP
;
1623 case kPageDownCharCode
:
1624 retval
= WXK_PAGEDOWN
;
1626 case kReturnCharCode
:
1627 retval
= WXK_RETURN
;
1629 case kFunctionKeyCharCode
:
1681 case kEscapeCharCode
:
1682 retval
= WXK_ESCAPE
;
1684 case kLeftArrowCharCode
:
1687 case kRightArrowCharCode
:
1688 retval
= WXK_RIGHT
;
1690 case kUpArrowCharCode
:
1693 case kDownArrowCharCode
:
1696 case kDeleteCharCode
:
1697 retval
= WXK_DELETE
;
1706 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1708 EventRecord
* ev
= (EventRecord
*) evr
;
1709 wxToolTip::RemoveToolTips() ;
1711 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1712 if ( HiWord( menuresult
) )
1714 if ( !s_macIsInModalLoop
)
1715 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1719 wxWindow
* focus
= wxWindow::FindFocus() ;
1721 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1724 // we must handle control keys the other way round, otherwise text content is updated too late
1725 // has not been handled -> perform default
1726 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1727 if ( control
&& control
->GetMacControl() != NULL
)
1731 keychar
= short(ev
->message
& charCodeMask
);
1732 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1733 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1740 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1742 EventRecord
* ev
= (EventRecord
*) evr
;
1743 wxToolTip::RemoveToolTips() ;
1745 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1746 if ( HiWord( menuresult
) )
1751 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1757 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1764 keychar
= short(keymessage
& charCodeMask
);
1765 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1767 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1769 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1770 // and look at the character after
1772 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1773 keychar
= short(keyInfo
& charCodeMask
);
1774 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1776 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1777 long realkeyval
= keyval
;
1778 if ( keyval
== keychar
)
1780 // 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)
1781 realkeyval
= short(keymessage
& charCodeMask
) ;
1782 keyval
= wxToupper( keyval
) ;
1785 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1786 bool handled
= false ;
1787 event
.m_shiftDown
= modifiers
& shiftKey
;
1788 event
.m_controlDown
= modifiers
& controlKey
;
1789 event
.m_altDown
= modifiers
& optionKey
;
1790 event
.m_metaDown
= modifiers
& cmdKey
;
1791 event
.m_keyCode
= keyval
;
1795 event
.m_timeStamp
= when
;
1796 event
.SetEventObject(focus
);
1797 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1798 if ( handled
&& event
.GetSkipped() )
1805 wxWindow
*ancestor
= focus
;
1808 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1811 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1812 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1815 if (ancestor
->IsTopLevel())
1817 ancestor
= ancestor
->GetParent();
1820 #endif // wxUSE_ACCEL
1824 event
.Skip( FALSE
) ;
1825 event
.SetEventType( wxEVT_CHAR
) ;
1827 event
.m_keyCode
= realkeyval
;
1829 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1830 if ( handled
&& event
.GetSkipped() )
1834 (keyval
== WXK_TAB
) &&
1835 // CS: copied the change below from wxGTK
1836 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1837 // have this style, yet choose not to process this particular TAB in which
1838 // case TAB must still work as a navigational character
1840 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
1842 (focus
->GetParent()) &&
1843 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
1845 wxNavigationKeyEvent new_event
;
1846 new_event
.SetEventObject( focus
);
1847 new_event
.SetDirection( !event
.ShiftDown() );
1848 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1849 new_event
.SetWindowChange( event
.ControlDown() );
1850 new_event
.SetCurrentFocus( focus
);
1851 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1852 if ( handled
&& new_event
.GetSkipped() )
1855 // backdoor handler for default return and command escape
1856 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1858 // if window is not having a focus still testing for default enter or cancel
1859 // TODO add the UMA version for ActiveNonFloatingWindow
1860 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1863 if ( keyval
== WXK_RETURN
)
1865 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1867 if ( def
&& def
->IsEnabled() )
1869 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1870 event
.SetEventObject(def
);
1871 def
->Command(event
);
1875 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1876 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1878 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1879 new_event
.SetEventObject( focus
);
1880 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1887 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1894 keychar
= short(keymessage
& charCodeMask
);
1895 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1896 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1898 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1899 // and look at the character after
1901 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1902 keychar
= short(keyInfo
& charCodeMask
);
1903 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1905 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1907 if ( keyval
== keychar
)
1909 keyval
= wxToupper( keyval
) ;
1911 bool handled
= false ;
1913 wxKeyEvent
event(wxEVT_KEY_UP
);
1914 event
.m_shiftDown
= modifiers
& shiftKey
;
1915 event
.m_controlDown
= modifiers
& controlKey
;
1916 event
.m_altDown
= modifiers
& optionKey
;
1917 event
.m_metaDown
= modifiers
& cmdKey
;
1918 event
.m_keyCode
= keyval
;
1922 event
.m_timeStamp
= when
;
1923 event
.SetEventObject(focus
);
1924 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1930 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
1932 EventRecord
* ev
= (EventRecord
*) evr
;
1933 WindowRef window
= (WindowRef
) ev
->message
;
1936 bool activate
= (ev
->modifiers
& activeFlag
) ;
1937 WindowClass wclass
;
1938 ::GetWindowClass ( window
, &wclass
) ;
1939 if ( wclass
== kFloatingWindowClass
)
1941 // if it is a floater we activate/deactivate the front non-floating window instead
1942 window
= ::FrontNonFloatingWindow() ;
1944 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1946 win
->MacActivate( ev
->when
, activate
) ;
1950 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
1952 EventRecord
* ev
= (EventRecord
*) evr
;
1953 WindowRef window
= (WindowRef
) ev
->message
;
1954 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1957 if ( !wxPendingDelete
.Member(win
) )
1958 win
->MacUpdate( ev
->when
) ;
1962 // since there is no way of telling this foreign window to update itself
1963 // we have to invalidate the update region otherwise we keep getting the same
1964 // event over and over again
1965 BeginUpdate( window
) ;
1966 EndUpdate( window
) ;
1970 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
1972 EventRecord
* ev
= (EventRecord
*) evr
;
1973 if ( HiWord( ev
->message
) != noErr
)
1977 SetPt( &point
, 100 , 100 ) ;
1979 err
= DIBadMount( point
, ev
->message
) ;
1980 wxASSERT( err
== noErr
) ;
1984 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
1986 EventRecord
* ev
= (EventRecord
*) evr
;
1987 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
1989 case suspendResumeMessage
:
1991 bool isResuming
= ev
->message
& resumeFlag
;
1992 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
1994 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
1997 WindowRef oldFrontWindow
= NULL
;
1998 WindowRef newFrontWindow
= NULL
;
2000 // in case we don't take care of activating ourselves, we have to synchronize
2001 // our idea of the active window with the process manager's - which it already activated
2003 if ( !doesActivate
)
2004 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2006 MacResume( convertClipboard
) ;
2008 newFrontWindow
= ::FrontNonFloatingWindow() ;
2010 if ( oldFrontWindow
)
2012 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2014 win
->MacActivate( ev
->when
, false ) ;
2016 if ( newFrontWindow
)
2018 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2020 win
->MacActivate( ev
->when
, true ) ;
2025 MacSuspend( convertClipboard
) ;
2029 case mouseMovedMessage
:
2033 wxWindow
* currentMouseWindow
= NULL
;
2035 if (s_captureWindow
)
2037 currentMouseWindow
= s_captureWindow
;
2041 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2042 ¤tMouseWindow
) ;
2045 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2047 wxMouseEvent event
;
2049 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2050 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2052 event
.m_leftDown
= isDown
&& !controlDown
;
2053 event
.m_middleDown
= FALSE
;
2054 event
.m_rightDown
= isDown
&& controlDown
;
2055 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2056 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2057 event
.m_altDown
= ev
->modifiers
& optionKey
;
2058 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2059 event
.m_x
= ev
->where
.h
;
2060 event
.m_y
= ev
->where
.v
;
2061 event
.m_timeStamp
= ev
->when
;
2062 event
.SetEventObject(this);
2064 if ( wxWindow::s_lastMouseWindow
)
2066 wxMouseEvent
eventleave(event
);
2067 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2068 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2069 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2071 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2073 if ( currentMouseWindow
)
2075 wxMouseEvent
evententer(event
);
2076 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2077 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2078 evententer
.SetEventObject( currentMouseWindow
) ;
2079 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2081 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2084 short windowPart
= inNoWindow
;
2086 if ( s_captureWindow
)
2088 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2089 windowPart
= inContent
;
2093 windowPart
= ::FindWindow(ev
->where
, &window
);
2100 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2102 win
->MacMouseMoved( ev
, windowPart
) ;
2109 UMAShowArrowCursor();
2119 UMAShowArrowCursor();
2130 void wxApp::MacHandleMouseMovedEvent(wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
2134 wxWindow
* currentMouseWindow
= NULL
;
2136 if (s_captureWindow
)
2138 currentMouseWindow
= s_captureWindow
;
2142 wxWindow::MacGetWindowFromPoint( wxPoint( x
, y
) , ¤tMouseWindow
) ;
2145 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2147 wxMouseEvent event
;
2149 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
2150 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
2152 event
.m_leftDown
= isDown
&& !controlDown
;
2154 event
.m_middleDown
= FALSE
;
2155 event
.m_rightDown
= isDown
&& controlDown
;
2157 event
.m_shiftDown
= modifiers
& shiftKey
;
2158 event
.m_controlDown
= modifiers
& controlKey
;
2159 event
.m_altDown
= modifiers
& optionKey
;
2160 event
.m_metaDown
= modifiers
& cmdKey
;
2164 event
.m_timeStamp
= timestamp
;
2166 if ( wxWindow::s_lastMouseWindow
)
2168 wxMouseEvent
eventleave(event
);
2169 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2170 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2171 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2174 wxToolTip::RelayEvent( wxWindow::s_lastMouseWindow
, eventleave
);
2175 #endif // wxUSE_TOOLTIPS
2176 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2178 if ( currentMouseWindow
)
2180 wxMouseEvent
evententer(event
);
2181 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2182 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2183 evententer
.SetEventObject( currentMouseWindow
) ;
2185 wxToolTip::RelayEvent( currentMouseWindow
, evententer
);
2186 #endif // wxUSE_TOOLTIPS
2187 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2189 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2192 short windowPart
= inNoWindow
;
2194 if ( s_captureWindow
)
2196 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2197 windowPart
= inContent
;
2201 Point pt
= { y
, x
} ;
2202 windowPart
= ::FindWindow(pt
, &window
);
2209 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2211 win
->MacFireMouseEvent( nullEvent
, x
, y
, modifiers
, timestamp
) ;
2218 UMAShowArrowCursor();
2228 UMAShowArrowCursor();
2235 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2237 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2238 wxFrame
* frame
= mbar
->GetFrame();
2239 wxCHECK_RET( mbar
!= NULL
&& frame
!= NULL
, wxT("error in menu item callback") );
2242 frame
->ProcessCommand(id
);
2247 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2250 return; // no menu item selected
2252 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2255 Str255 deskAccessoryName
;
2258 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2259 GetPort(&savedPort
);
2260 OpenDeskAcc(deskAccessoryName
);
2267 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2268 MacHandleMenuCommand( id
) ;