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
= 0 ;
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
, 20000 , 40 ) ;
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
) ;
1544 win
->MacMouseDown( ev
, windowPart
) ;
1545 ::SelectWindow( window
) ;
1551 win
->MacMouseDown( ev
, windowPart
) ;
1559 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1561 EventRecord
* ev
= (EventRecord
*) evr
;
1564 short windowPart
= inNoWindow
;
1565 if ( wxTheApp
->s_captureWindow
)
1567 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1568 windowPart
= inContent
;
1572 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1583 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1585 win
->MacMouseUp( ev
, windowPart
) ;
1593 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1594 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1599 case kHomeCharCode
:
1602 case kEnterCharCode
:
1603 retval
= WXK_RETURN
;
1608 case kHelpCharCode
:
1611 case kBackspaceCharCode
:
1617 case kPageUpCharCode
:
1618 retval
= WXK_PAGEUP
;
1620 case kPageDownCharCode
:
1621 retval
= WXK_PAGEDOWN
;
1623 case kReturnCharCode
:
1624 retval
= WXK_RETURN
;
1626 case kFunctionKeyCharCode
:
1678 case kEscapeCharCode
:
1679 retval
= WXK_ESCAPE
;
1681 case kLeftArrowCharCode
:
1684 case kRightArrowCharCode
:
1685 retval
= WXK_RIGHT
;
1687 case kUpArrowCharCode
:
1690 case kDownArrowCharCode
:
1693 case kDeleteCharCode
:
1694 retval
= WXK_DELETE
;
1703 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1705 EventRecord
* ev
= (EventRecord
*) evr
;
1706 wxToolTip::RemoveToolTips() ;
1708 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1709 if ( HiWord( menuresult
) )
1711 if ( !s_macIsInModalLoop
)
1712 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1716 wxWindow
* focus
= wxWindow::FindFocus() ;
1718 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1721 // we must handle control keys the other way round, otherwise text content is updated too late
1722 // has not been handled -> perform default
1723 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1724 if ( control
&& control
->GetMacControl() != NULL
)
1728 keychar
= short(ev
->message
& charCodeMask
);
1729 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1730 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1737 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1739 EventRecord
* ev
= (EventRecord
*) evr
;
1740 wxToolTip::RemoveToolTips() ;
1742 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1743 if ( HiWord( menuresult
) )
1748 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1754 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1761 keychar
= short(keymessage
& charCodeMask
);
1762 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1764 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1766 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1767 // and look at the character after
1769 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1770 keychar
= short(keyInfo
& charCodeMask
);
1771 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1773 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1774 long realkeyval
= keyval
;
1775 if ( keyval
== keychar
)
1777 // 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)
1778 realkeyval
= short(keymessage
& charCodeMask
) ;
1779 keyval
= wxToupper( keyval
) ;
1782 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1783 bool handled
= false ;
1784 event
.m_shiftDown
= modifiers
& shiftKey
;
1785 event
.m_controlDown
= modifiers
& controlKey
;
1786 event
.m_altDown
= modifiers
& optionKey
;
1787 event
.m_metaDown
= modifiers
& cmdKey
;
1788 event
.m_keyCode
= keyval
;
1792 event
.m_timeStamp
= when
;
1793 event
.SetEventObject(focus
);
1794 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1795 if ( handled
&& event
.GetSkipped() )
1802 wxWindow
*ancestor
= focus
;
1805 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1808 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1809 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1812 if (ancestor
->IsTopLevel())
1814 ancestor
= ancestor
->GetParent();
1817 #endif // wxUSE_ACCEL
1821 event
.Skip( FALSE
) ;
1822 event
.SetEventType( wxEVT_CHAR
) ;
1824 event
.m_keyCode
= realkeyval
;
1826 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1827 if ( handled
&& event
.GetSkipped() )
1831 (keyval
== WXK_TAB
) &&
1832 // CS: copied the change below from wxGTK
1833 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1834 // have this style, yet choose not to process this particular TAB in which
1835 // case TAB must still work as a navigational character
1837 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
1839 (focus
->GetParent()) &&
1840 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
1842 wxNavigationKeyEvent new_event
;
1843 new_event
.SetEventObject( focus
);
1844 new_event
.SetDirection( !event
.ShiftDown() );
1845 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1846 new_event
.SetWindowChange( event
.ControlDown() );
1847 new_event
.SetCurrentFocus( focus
);
1848 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1849 if ( handled
&& new_event
.GetSkipped() )
1852 // backdoor handler for default return and command escape
1853 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1855 // if window is not having a focus still testing for default enter or cancel
1856 // TODO add the UMA version for ActiveNonFloatingWindow
1857 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1860 if ( keyval
== WXK_RETURN
)
1862 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1864 if ( def
&& def
->IsEnabled() )
1866 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1867 event
.SetEventObject(def
);
1868 def
->Command(event
);
1872 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1873 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1875 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1876 new_event
.SetEventObject( focus
);
1877 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1884 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1891 keychar
= short(keymessage
& charCodeMask
);
1892 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1893 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1895 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1896 // and look at the character after
1898 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1899 keychar
= short(keyInfo
& charCodeMask
);
1900 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1902 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1904 if ( keyval
== keychar
)
1906 keyval
= wxToupper( keyval
) ;
1908 bool handled
= false ;
1910 wxKeyEvent
event(wxEVT_KEY_UP
);
1911 event
.m_shiftDown
= modifiers
& shiftKey
;
1912 event
.m_controlDown
= modifiers
& controlKey
;
1913 event
.m_altDown
= modifiers
& optionKey
;
1914 event
.m_metaDown
= modifiers
& cmdKey
;
1915 event
.m_keyCode
= keyval
;
1919 event
.m_timeStamp
= when
;
1920 event
.SetEventObject(focus
);
1921 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1927 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
1929 EventRecord
* ev
= (EventRecord
*) evr
;
1930 WindowRef window
= (WindowRef
) ev
->message
;
1933 bool activate
= (ev
->modifiers
& activeFlag
) ;
1934 WindowClass wclass
;
1935 ::GetWindowClass ( window
, &wclass
) ;
1936 if ( wclass
== kFloatingWindowClass
)
1938 // if it is a floater we activate/deactivate the front non-floating window instead
1939 window
= ::FrontNonFloatingWindow() ;
1941 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1943 win
->MacActivate( ev
->when
, activate
) ;
1947 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
1949 EventRecord
* ev
= (EventRecord
*) evr
;
1950 WindowRef window
= (WindowRef
) ev
->message
;
1951 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1954 if ( !wxPendingDelete
.Member(win
) )
1955 win
->MacUpdate( ev
->when
) ;
1959 // since there is no way of telling this foreign window to update itself
1960 // we have to invalidate the update region otherwise we keep getting the same
1961 // event over and over again
1962 BeginUpdate( window
) ;
1963 EndUpdate( window
) ;
1967 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
1969 EventRecord
* ev
= (EventRecord
*) evr
;
1970 if ( HiWord( ev
->message
) != noErr
)
1974 SetPt( &point
, 100 , 100 ) ;
1976 err
= DIBadMount( point
, ev
->message
) ;
1977 wxASSERT( err
== noErr
) ;
1981 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
1983 EventRecord
* ev
= (EventRecord
*) evr
;
1984 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
1986 case suspendResumeMessage
:
1988 bool isResuming
= ev
->message
& resumeFlag
;
1989 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
1991 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
1994 WindowRef oldFrontWindow
= NULL
;
1995 WindowRef newFrontWindow
= NULL
;
1997 // in case we don't take care of activating ourselves, we have to synchronize
1998 // our idea of the active window with the process manager's - which it already activated
2000 if ( !doesActivate
)
2001 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2003 MacResume( convertClipboard
) ;
2005 newFrontWindow
= ::FrontNonFloatingWindow() ;
2007 if ( oldFrontWindow
)
2009 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2011 win
->MacActivate( ev
->when
, false ) ;
2013 if ( newFrontWindow
)
2015 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2017 win
->MacActivate( ev
->when
, true ) ;
2022 MacSuspend( convertClipboard
) ;
2026 case mouseMovedMessage
:
2030 wxWindow
* currentMouseWindow
= NULL
;
2032 if (s_captureWindow
)
2034 currentMouseWindow
= s_captureWindow
;
2038 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2039 ¤tMouseWindow
) ;
2042 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2044 wxMouseEvent event
;
2046 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2047 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2049 event
.m_leftDown
= isDown
&& !controlDown
;
2050 event
.m_middleDown
= FALSE
;
2051 event
.m_rightDown
= isDown
&& controlDown
;
2052 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2053 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2054 event
.m_altDown
= ev
->modifiers
& optionKey
;
2055 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2056 event
.m_x
= ev
->where
.h
;
2057 event
.m_y
= ev
->where
.v
;
2058 event
.m_timeStamp
= ev
->when
;
2059 event
.SetEventObject(this);
2061 if ( wxWindow::s_lastMouseWindow
)
2063 wxMouseEvent
eventleave(event
);
2064 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2065 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2066 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2068 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2070 if ( currentMouseWindow
)
2072 wxMouseEvent
evententer(event
);
2073 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2074 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2075 evententer
.SetEventObject( currentMouseWindow
) ;
2076 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2078 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2081 short windowPart
= inNoWindow
;
2083 if ( s_captureWindow
)
2085 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2086 windowPart
= inContent
;
2090 windowPart
= ::FindWindow(ev
->where
, &window
);
2097 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2099 win
->MacMouseMoved( ev
, windowPart
) ;
2106 UMAShowArrowCursor();
2116 UMAShowArrowCursor();
2127 void wxApp::MacHandleMouseMovedEvent(wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
2131 wxWindow
* currentMouseWindow
= NULL
;
2133 if (s_captureWindow
)
2135 currentMouseWindow
= s_captureWindow
;
2139 wxWindow::MacGetWindowFromPoint( wxPoint( x
, y
) , ¤tMouseWindow
) ;
2142 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2144 wxMouseEvent event
;
2146 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
2147 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
2149 event
.m_leftDown
= isDown
&& !controlDown
;
2151 event
.m_middleDown
= FALSE
;
2152 event
.m_rightDown
= isDown
&& controlDown
;
2154 event
.m_shiftDown
= modifiers
& shiftKey
;
2155 event
.m_controlDown
= modifiers
& controlKey
;
2156 event
.m_altDown
= modifiers
& optionKey
;
2157 event
.m_metaDown
= modifiers
& cmdKey
;
2161 event
.m_timeStamp
= timestamp
;
2163 if ( wxWindow::s_lastMouseWindow
)
2165 wxMouseEvent
eventleave(event
);
2166 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2167 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2168 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2171 wxToolTip::RelayEvent( wxWindow::s_lastMouseWindow
, eventleave
);
2172 #endif // wxUSE_TOOLTIPS
2173 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2175 if ( currentMouseWindow
)
2177 wxMouseEvent
evententer(event
);
2178 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2179 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2180 evententer
.SetEventObject( currentMouseWindow
) ;
2182 wxToolTip::RelayEvent( currentMouseWindow
, evententer
);
2183 #endif // wxUSE_TOOLTIPS
2184 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2186 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2189 short windowPart
= inNoWindow
;
2191 if ( s_captureWindow
)
2193 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2194 windowPart
= inContent
;
2198 Point pt
= { y
, x
} ;
2199 windowPart
= ::FindWindow(pt
, &window
);
2206 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2208 win
->MacFireMouseEvent( nullEvent
, x
, y
, modifiers
, timestamp
) ;
2215 UMAShowArrowCursor();
2225 UMAShowArrowCursor();
2232 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2234 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2235 wxFrame
* frame
= mbar
->GetFrame();
2236 wxCHECK_RET( mbar
!= NULL
&& frame
!= NULL
, wxT("error in menu item callback") );
2239 frame
->ProcessCommand(id
);
2244 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2247 return; // no menu item selected
2249 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2252 Str255 deskAccessoryName
;
2255 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2256 GetPort(&savedPort
);
2257 OpenDeskAcc(deskAccessoryName
);
2264 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2265 MacHandleMenuCommand( id
) ;