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
;
72 extern size_t g_numberOfThreads
;
74 // statics for implementation
76 static bool s_inYield
= FALSE
;
79 static bool s_inReceiveEvent
= FALSE
;
80 static EventTime sleepTime
= kEventDurationNoWait
;
82 static long sleepTime
= 0 ;
85 #if !USE_SHARED_LIBRARY
86 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
87 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
88 EVT_IDLE(wxApp::OnIdle
)
89 EVT_END_SESSION(wxApp::OnEndSession
)
90 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
95 const short kMacMinHeap
= (29 * 1024) ;
96 // platform specific static variables
98 const short kwxMacMenuBarResource
= 1 ;
99 const short kwxMacAppleMenuId
= 1 ;
101 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
102 wxWindow
* wxApp::s_captureWindow
= NULL
;
103 int wxApp::s_lastMouseDown
= 0 ;
104 long wxApp::sm_lastMessageTime
= 0;
105 long wxApp::s_lastModifiers
= 0 ;
108 bool wxApp::s_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 // Normally we're not a plugin
115 bool wxApp::sm_isEmbedded
= false;
116 //----------------------------------------------------------------------
117 // Core Apple Event Support
118 //----------------------------------------------------------------------
120 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
121 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
122 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
123 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
125 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
127 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
130 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
132 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
135 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
137 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
140 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
142 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
145 // AEODoc Calls MacOpenFile on each of the files passed
147 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
151 DescType returnedType
;
157 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
161 err
= AECountItems(&docList
, &itemsInList
);
165 ProcessSerialNumber PSN
;
166 PSN
.highLongOfPSN
= 0 ;
167 PSN
.lowLongOfPSN
= kCurrentProcess
;
168 SetFrontProcess( &PSN
) ;
170 for (i
= 1; i
<= itemsInList
; i
++) {
171 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
172 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
173 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
179 // AEPDoc Calls MacPrintFile on each of the files passed
181 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
185 DescType returnedType
;
191 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
195 err
= AECountItems(&docList
, &itemsInList
);
199 ProcessSerialNumber PSN
;
200 PSN
.highLongOfPSN
= 0 ;
201 PSN
.lowLongOfPSN
= kCurrentProcess
;
202 SetFrontProcess( &PSN
) ;
204 for (i
= 1; i
<= itemsInList
; i
++) {
205 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
206 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
207 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
213 // AEOApp calls MacNewFile
215 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
221 // AEQuit attempts to quit the application
223 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
225 wxWindow
* win
= GetTopWindow() ;
237 //----------------------------------------------------------------------
238 // Support Routines linking the Mac...File Calls to the Document Manager
239 //----------------------------------------------------------------------
241 void wxApp::MacOpenFile(const wxString
& fileName
)
243 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
245 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
248 void wxApp::MacPrintFile(const wxString
& fileName
)
250 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
253 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
256 wxView
* view
= doc
->GetFirstView() ;
259 wxPrintout
*printout
= view
->OnCreatePrintout();
263 printer
.Print(view
->GetFrame(), printout
, TRUE
);
269 doc
->DeleteAllViews();
270 dm
->RemoveDocument(doc
) ;
276 void wxApp::MacNewFile()
280 //----------------------------------------------------------------------
281 // Carbon Event Handler
282 //----------------------------------------------------------------------
286 static const EventTypeSpec eventList
[] =
288 { kEventClassCommand
, kEventProcessCommand
} ,
289 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
291 { kEventClassMenu
, kEventMenuOpening
},
292 { kEventClassMenu
, kEventMenuClosed
},
294 { kEventClassApplication
, kEventAppActivated
} ,
295 { kEventClassApplication
, kEventAppDeactivated
} ,
296 // handling the quit event is not recommended by apple
297 // rather using the quit apple event - which we do
299 { kEventClassAppleEvent
, kEventAppleEvent
} ,
301 { kEventClassMouse
, kEventMouseDown
} ,
302 { kEventClassMouse
, kEventMouseMoved
} ,
303 { kEventClassMouse
, kEventMouseUp
} ,
304 { kEventClassMouse
, kEventMouseDragged
} ,
308 static pascal OSStatus
309 MenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
311 // FIXME: this doesn't work for multiple windows
312 wxWindow
*win
= wxTheApp
->GetTopWindow();
315 // VZ: we could find the menu from its handle here by examining all
316 // the menus in the menu bar recursively but knowing that neither
317 // wxMSW nor wxGTK do it why bother...
321 GetEventParameter(event
,
322 kEventParamDirectObject
,
324 sizeof(menuRef
), NULL
,
328 wxMenuEvent
wxevent(GetEventKind(event
) == kEventMenuOpening
331 wxevent
.SetEventObject(win
);
333 (void)win
->GetEventHandler()->ProcessEvent(wxevent
);
336 return eventNotHandledErr
;
339 // due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop
340 // but have to use ReceiveNextEvent dealing with events manually, therefore we also have
341 // deal with clicks in the menu bar explicitely
343 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
) ;
345 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
347 OSStatus result
= eventNotHandledErr
;
350 UInt32 modifiers
= 0;
351 EventMouseButton button
= 0 ;
354 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
355 sizeof( Point
), NULL
, &point
);
356 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
,
357 sizeof( UInt32
), NULL
, &modifiers
);
358 GetEventParameter( event
, kEventParamMouseButton
, typeMouseButton
, NULL
,
359 sizeof( EventMouseButton
), NULL
, &button
);
360 GetEventParameter( event
, kEventParamClickCount
, typeUInt32
, NULL
,
361 sizeof( UInt32
), NULL
, &click
);
363 if ( button
== 0 || GetEventKind( event
) == kEventMouseUp
)
364 modifiers
+= btnState
;
367 switch( GetEventKind(event
) )
369 case kEventMouseDown
:
373 short windowPart
= ::FindWindow(point
, &window
);
375 if ( windowPart
== inMenuBar
)
377 MenuSelect( point
) ;
382 case kEventMouseDragged
:
385 if ( wxTheApp
->s_captureWindow
)
386 wxMacWindowEventHandler( handler
, event
, (void*) wxTheApp
->s_captureWindow
->MacGetTopLevelWindow() ) ;
389 case kEventMouseMoved
:
391 wxTheApp
->MacHandleMouseMovedEvent( point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
401 static pascal OSStatus
CommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
403 OSStatus result
= eventNotHandledErr
;
407 GetEventParameter( event
, kEventParamDirectObject
, typeHICommand
, NULL
,
408 sizeof( HICommand
), NULL
, &command
);
410 MenuCommand id
= command
.commandID
;
411 if ( id
== kHICommandPreferences
)
412 id
= wxApp::s_macPreferencesMenuItemId
;
414 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
415 wxMenu
* menu
= NULL
;
416 wxMenuItem
* item
= NULL
;
420 item
= mbar
->FindItem( id
, &menu
) ;
421 // it is not 100 % sure that an menu of id 0 is really ours, safety check
422 if ( id
== 0 && menu
!= NULL
&& menu
->GetHMenu() != command
.menu
.menuRef
)
429 if ( item
== NULL
|| menu
== NULL
|| mbar
== NULL
)
432 switch( GetEventKind( event
) )
434 case kEventProcessCommand
:
436 if (item
->IsCheckable())
438 item
->Check( !item
->IsChecked() ) ;
441 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
445 case kEventCommandUpdateStatus
:
446 // eventually trigger an updateui round
456 static pascal OSStatus
ApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
458 OSStatus result
= eventNotHandledErr
;
459 switch ( GetEventKind( event
) )
461 case kEventAppActivated
:
464 wxTheApp
->MacResume( true ) ;
468 case kEventAppDeactivated
:
471 wxTheApp
->MacSuspend( true ) ;
481 pascal OSStatus
wxAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
483 OSStatus result
= eventNotHandledErr
;
484 switch( GetEventClass( event
) )
486 case kEventClassCommand
:
487 result
= CommandEventHandler( handler
, event
, data
) ;
489 case kEventClassApplication
:
490 result
= ApplicationEventHandler( handler
, event
, data
) ;
492 case kEventClassMenu
:
493 result
= MenuEventHandler( handler
, event
, data
) ;
495 case kEventClassMouse
:
496 result
= MouseEventHandler( handler
, event
, data
) ;
498 case kEventClassAppleEvent
:
501 wxMacConvertEventToRecord( event
, &rec
) ;
502 result
= AEProcessAppleEvent( &rec
) ;
512 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler
)
516 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
517 // we know it's there ;-)
518 WXIMPORT
char std::__throws_bad_alloc
;
521 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
527 UMAInitToolbox( 4, sm_isEmbedded
) ;
528 SetEventMask( everyEvent
) ;
529 UMAShowWatchCursor() ;
531 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
532 // open shared library resources from here since we don't have
533 // __wxinitialize in Mach-O shared libraries
534 wxStAppResource::OpenSharedLibraryResource(NULL
);
538 // test the minimal configuration necessary
544 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
546 error
= kMacSTRWrongMachine
;
548 else if (theMachine
< gestaltMacPlus
)
550 error
= kMacSTRWrongMachine
;
552 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
554 error
= kMacSTROldSystem
;
556 else if ( theSystem
< 0x0860 )
558 error
= kMacSTROldSystem
;
560 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
562 error
= kMacSTRSmallSize
;
568 if ( !UMAHasAppearance() )
570 error = kMacSTRNoPre8Yet ;
576 // if we encountered any problems so far, give the error code and exit immediately
580 wxStAppResource resload
;
584 GetIndString(message
, 128, error
);
585 UMAShowArrowCursor() ;
586 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
587 itemHit
= Alert(128, nil
);
592 # if __option(profile)
593 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
598 // now avoid exceptions thrown for new (bad_alloc)
599 // FIXME CS for some changes outside wxMac does not compile anymore
601 std::__throws_bad_alloc
= 0 ;
606 s_macCursorRgn
= ::NewRgn() ;
608 // Mac OS X passes a process serial number command line argument when
609 // the application is launched from the Finder. This argument must be
610 // removed from the command line arguments before being handled by the
611 // application (otherwise applications would need to handle it)
614 static const wxChar
*ARG_PSN
= _T("-psn_");
615 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
617 // remove this argument
619 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
623 if ( !wxAppBase::Initialize(argc
, argv
) )
626 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
627 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
629 wxMacCreateNotifierTable() ;
631 UMAShowArrowCursor() ;
636 bool wxApp::OnInitGui()
638 if( !wxAppBase::OnInitGui() )
642 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
646 InstallApplicationEventHandler(
647 GetwxAppEventHandlerUPP(),
648 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
654 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
655 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
656 NewAEEventHandlerUPP(AEHandleODoc
) ,
658 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
659 NewAEEventHandlerUPP(AEHandleOApp
) ,
661 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
662 NewAEEventHandlerUPP(AEHandlePDoc
) ,
664 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
665 NewAEEventHandlerUPP(AEHandleQuit
) ,
668 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
669 NewAEEventHandlerProc(AEHandleODoc
) ,
671 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
672 NewAEEventHandlerProc(AEHandleOApp
) ,
674 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
675 NewAEEventHandlerProc(AEHandlePDoc
) ,
677 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
678 NewAEEventHandlerProc(AEHandleQuit
) ,
686 void wxApp::CleanUp()
688 wxToolTip::RemoveToolTips() ;
690 // One last chance for pending objects to be cleaned up
691 wxTheApp
->DeletePendingObjects();
693 wxMacDestroyNotifierTable() ;
695 delete wxWinMacWindowList
;
696 wxWinMacWindowList
= NULL
;
698 delete wxWinMacControlList
;
699 wxWinMacControlList
= NULL
;
702 # if __option(profile)
703 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
708 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
709 // close shared library resources from here since we don't have
710 // __wxterminate in Mach-O shared libraries
711 wxStAppResource::CloseSharedLibraryResource();
714 UMACleanupToolbox() ;
715 if (s_macCursorRgn
) {
716 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
723 wxAppBase::CleanUp();
726 //----------------------------------------------------------------------
727 // misc initialization stuff
728 //----------------------------------------------------------------------
730 // extern variable for shared library resource id
731 // need to be able to find it with NSLookupAndBindSymbol
732 short gSharedLibraryResource
= kResFileNotOpened
;
734 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
735 CFBundleRef gSharedLibraryBundle
= NULL
;
736 #endif /* WXMAKINGDLL_CORE && __DARWIN__ */
738 wxStAppResource::wxStAppResource()
740 m_currentRefNum
= CurResFile() ;
741 if ( gSharedLibraryResource
!= kResFileNotOpened
)
743 UseResFile( gSharedLibraryResource
) ;
747 wxStAppResource::~wxStAppResource()
749 if ( m_currentRefNum
!= kResFileNotOpened
)
751 UseResFile( m_currentRefNum
) ;
755 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
757 gSharedLibraryResource
= kResFileNotOpened
;
759 #ifdef WXMAKINGDLL_CORE
760 if ( initBlock
!= NULL
) {
761 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
762 FSSpec
*fileSpec
= NULL
;
764 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
765 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
767 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
768 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
771 if (fileSpec
!= NULL
) {
772 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
777 // Open the shared library resource file if it is not yet open
780 const char *theLibPath
;
782 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
783 if (gSharedLibraryBundle
!= NULL
) {
784 // wxWindows has been bundled into a framework
785 // load the framework resources
787 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
790 // wxWindows is a simple dynamic shared library
791 // load the resources from the data fork of a separate resource file
795 OSErr theErr
= noErr
;
797 // get the library path
798 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
799 theModule
= NSModuleForSymbol(theSymbol
);
800 theLibPath
= NSLibraryNameForModule(theModule
);
802 // if we call wxLogDebug from here then, as wxTheApp hasn't been
803 // created yet when we're called from wxApp::Initialize(), wxLog
804 // is going to create a default stderr-based log target instead of
805 // the expected normal GUI one -- don't do it, if we really want
806 // to see this message just use fprintf() here
808 wxLogDebug( wxT("wxMac library installation name is '%s'"),
812 // allocate copy to replace .dylib.* extension with .rsrc
813 if (theLibPath
!= NULL
) {
814 theResPath
= theLibPath
;
815 // replace '_core' with '' in case of multi-lib build
816 theResPath
.Replace(wxT("_core"), wxEmptyString
);
817 // replace ".dylib" shared library extension with ".rsrc"
818 theResPath
.Replace(wxT(".dylib"), wxT(".rsrc"));
819 // Find the begining of the filename
820 theName
= theResPath
.AfterLast('/');
823 wxLogDebug( wxT("wxMac resources file name is '%s'"),
824 theResPath
.mb_str() );
827 theErr
= FSPathMakeRef((UInt8
*) theResPath
.mb_str(), &theResRef
, false);
828 if (theErr
!= noErr
) {
829 // try in current directory (using name only)
830 theErr
= FSPathMakeRef((UInt8
*) theName
.mb_str(), &theResRef
, false);
833 // open the resource file
834 if (theErr
== noErr
) {
835 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
836 &gSharedLibraryResource
);
838 if (theErr
!= noErr
) {
841 wxT("unable to open wxMac resource file '%s'\n"),
842 theResPath
.mb_str() );
843 #endif // __WXDEBUG__
848 #endif /* __DARWIN__ */
850 #endif /* WXMAKINGDLL_CORE */
853 void wxStAppResource::CloseSharedLibraryResource()
855 #ifdef WXMAKINGDLL_CORE
856 // Close the shared library resource file
857 if (gSharedLibraryResource
!= kResFileNotOpened
) {
859 if (gSharedLibraryBundle
!= NULL
) {
860 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
861 gSharedLibraryResource
);
862 gSharedLibraryBundle
= NULL
;
865 #endif /* __DARWIN__ */
867 CloseResFile(gSharedLibraryResource
);
869 gSharedLibraryResource
= kResFileNotOpened
;
871 #endif /* WXMAKINGDLL_CORE */
874 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
876 // for shared libraries we have to manually get the correct resource
877 // ref num upon initializing and releasing when terminating, therefore
878 // the __wxinitialize and __wxterminate must be used
881 void __sinit(void); /* (generated by linker) */
882 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
883 pascal void __terminate(void);
886 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
888 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
889 return __initialize( theInitBlock
) ;
892 pascal void __wxterminate(void)
894 wxStAppResource::CloseSharedLibraryResource() ;
898 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
902 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
904 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
905 OSStatus err
= noErr
;
908 switch( GetEventClass( event
) )
910 case kEventClassKeyboard
:
913 switch( GetEventKind(event
) )
915 case kEventRawKeyDown
:
916 rec
->what
= keyDown
;
918 case kEventRawKeyRepeat
:
919 rec
->what
= autoKey
;
921 case kEventRawKeyUp
:
924 case kEventRawKeyModifiersChanged
:
925 rec
->what
= nullEvent
;
934 unsigned char charCode
;
936 GetMouse( &rec
->where
) ;
938 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
939 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
940 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
941 rec
->modifiers
= modifiers
;
942 rec
->message
= (keyCode
<< 8 ) + charCode
;
946 case kEventClassTextInput
:
948 switch( GetEventKind( event
) )
950 case kEventTextInputUnicodeForKeyEvent
:
953 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
957 unsigned char charCode
;
959 GetMouse( &rec
->where
) ;
960 rec
->what
= keyDown
;
961 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
962 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
963 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
964 rec
->modifiers
= modifiers
;
965 rec
->message
= (keyCode
<< 8 ) + charCode
;
981 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
983 OSStatus result = eventNotHandledErr ;
986 switch ( GetEventClass( event ) )
988 case kEventClassKeyboard :
989 if ( wxMacConvertEventToRecord( event , &rec ) )
991 wxTheApp->MacHandleModifierEvents( &rec ) ;
992 wxTheApp->MacHandleOneEvent( &rec ) ;
996 case kEventClassTextInput :
997 if ( wxMacConvertEventToRecord( event , &rec ) )
999 wxTheApp->MacHandleModifierEvents( &rec ) ;
1000 wxTheApp->MacHandleOneEvent( &rec ) ;
1014 m_printMode
= wxPRINT_WINDOWS
;
1017 m_macCurrentEvent
= NULL
;
1019 m_macCurrentEventHandlerCallRef
= NULL
;
1023 int wxApp::MainLoop()
1035 void wxApp::ExitMainLoop()
1037 m_keepGoing
= FALSE
;
1040 // Is a message/event pending?
1041 bool wxApp::Pending()
1044 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1048 return EventAvail( everyEvent
, &event
) ;
1052 // Dispatch a message.
1053 bool wxApp::Dispatch()
1060 void wxApp::OnIdle(wxIdleEvent
& event
)
1062 wxAppBase::OnIdle(event
);
1064 // If they are pending events, we must process them: pending events are
1065 // either events to the threads other than main or events posted with
1066 // wxPostEvent() functions
1067 wxMacProcessNotifierAndPendingEvents();
1069 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1070 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1073 void wxApp::WakeUpIdle()
1084 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1087 GetTopWindow()->Close(TRUE
);
1090 // Default behaviour: close the application with prompts. The
1091 // user can veto the close, and therefore the end session.
1092 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1096 if (!GetTopWindow()->Close(!event
.CanVeto()))
1101 extern "C" void wxCYield() ;
1107 // Yield to other processes
1109 bool wxApp::Yield(bool onlyIfNeeded
)
1113 if ( !onlyIfNeeded
)
1115 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1124 YieldToAnyThread() ;
1126 // by definition yield should handle all non-processed events
1130 OSStatus status
= noErr
;
1133 s_inReceiveEvent
= true ;
1134 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1135 s_inReceiveEvent
= false ;
1137 if ( status
== eventLoopTimedOutErr
)
1139 // make sure next time the event loop will trigger idle events
1140 sleepTime
= kEventDurationNoWait
;
1142 else if ( status
== eventLoopQuitErr
)
1144 // according to QA1061 this may also occur when a WakeUp Process
1149 MacHandleOneEvent( theEvent
) ;
1150 ReleaseEvent(theEvent
);
1152 } while( status
== noErr
) ;
1156 // having a larger value here leads to large performance slowdowns
1157 // so we cannot give background apps more processor time here
1158 // we do so however having a large sleep value in the main event loop
1161 while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1163 MacHandleModifierEvents( &event
) ;
1164 MacHandleOneEvent( &event
);
1165 if ( event
.what
!= kHighLevelEvent
)
1166 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1168 MacHandleModifierEvents( &event
) ;
1171 wxMacProcessNotifierAndPendingEvents() ;
1177 // platform specifics
1179 void wxApp::MacSuspend( bool convertClipboard
)
1182 // we have to deactive the top level windows manually
1184 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1187 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1189 #if 0 // having problems right now with that
1190 if (!win
->HasFlag(wxSTAY_ON_TOP
))
1193 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1195 node
= node
->GetNext();
1198 ::HideFloatingWindows() ;
1200 s_lastMouseDown
= 0 ;
1202 if( convertClipboard
)
1204 MacConvertPrivateToPublicScrap() ;
1208 extern wxList wxModalDialogs
;
1210 void wxApp::MacResume( bool convertClipboard
)
1212 s_lastMouseDown
= 0 ;
1213 if( convertClipboard
)
1215 MacConvertPublicToPrivateScrap() ;
1219 ::ShowFloatingWindows() ;
1220 // raise modal dialogs in case a non modal window was selected to activate the app
1222 wxNode
* node
= wxModalDialogs
.GetFirst();
1225 wxDialog
* dialog
= (wxDialog
*) node
->GetData();
1228 node
= node
->GetNext();
1233 void wxApp::MacConvertPrivateToPublicScrap()
1237 void wxApp::MacConvertPublicToPrivateScrap()
1241 void wxApp::MacDoOneEvent()
1246 s_inReceiveEvent
= true ;
1247 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1248 s_inReceiveEvent
= false ;
1249 if ( status
== eventLoopTimedOutErr
)
1251 if ( wxTheApp
->ProcessIdle() )
1252 sleepTime
= kEventDurationNoWait
;
1255 if (g_numberOfThreads
)
1257 sleepTime
= kEventDurationNoWait
;
1261 sleepTime
= kEventDurationSecond
;
1265 else if ( status
== eventLoopQuitErr
)
1267 // according to QA1061 this may also occur when a WakeUp Process
1272 MacHandleOneEvent( theEvent
) ;
1273 ReleaseEvent(theEvent
);
1274 sleepTime
= kEventDurationNoWait
;
1279 EventMask eventMask
= everyEvent
;
1281 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1283 MacHandleModifierEvents( &event
) ;
1284 MacHandleOneEvent( &event
);
1288 MacHandleModifierEvents( &event
) ;
1290 WindowPtr window
= ::FrontWindow() ;
1292 ::IdleControls( window
) ;
1294 if ( wxTheApp
->ProcessIdle() )
1295 sleepTime
= kEventDurationNoWait
;
1298 if (g_numberOfThreads
)
1300 sleepTime
= kEventDurationNoWait
;
1304 sleepTime
= kEventDurationSecond
;
1308 if ( event
.what
!= kHighLevelEvent
)
1309 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1313 DeletePendingObjects() ;
1314 wxMacProcessNotifierAndPendingEvents() ;
1317 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1319 // Override to process unhandled events as you please
1322 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1325 EventTargetRef theTarget
;
1326 theTarget
= GetEventDispatcherTarget();
1327 m_macCurrentEvent
= evr
;
1328 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1329 if(status
== eventNotHandledErr
)
1331 MacHandleUnhandledEvent(evr
);
1334 EventRecord
* ev
= (EventRecord
*) evr
;
1335 m_macCurrentEvent
= ev
;
1337 wxApp::sm_lastMessageTime
= ev
->when
;
1342 MacHandleMouseDownEvent( ev
) ;
1343 if ( ev
->modifiers
& controlKey
)
1344 s_lastMouseDown
= 2;
1346 s_lastMouseDown
= 1;
1349 if ( s_lastMouseDown
== 2 )
1351 ev
->modifiers
|= controlKey
;
1355 ev
->modifiers
&= ~controlKey
;
1357 MacHandleMouseUpEvent( ev
) ;
1358 s_lastMouseDown
= 0;
1361 MacHandleActivateEvent( ev
) ;
1364 // In embedded mode we first let the UnhandledEvent function
1365 // try to handle the update event. If we handle it ourselves
1366 // first and then pass it on, the host's windows won't update.
1367 MacHandleUnhandledEvent(ev
);
1368 MacHandleUpdateEvent( ev
) ;
1372 MacHandleKeyDownEvent( ev
) ;
1375 MacHandleKeyUpEvent( ev
) ;
1378 MacHandleDiskEvent( ev
) ;
1381 MacHandleOSEvent( ev
) ;
1383 case kHighLevelEvent
:
1384 MacHandleHighLevelEvent( ev
) ;
1390 wxMacProcessNotifierAndPendingEvents() ;
1394 bool s_macIsInModalLoop
= false ;
1396 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1398 EventRecord
* ev
= (EventRecord
*) evr
;
1399 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1401 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1403 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1404 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1405 event
.m_altDown
= ev
->modifiers
& optionKey
;
1406 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1408 event
.m_x
= ev
->where
.h
;
1409 event
.m_y
= ev
->where
.v
;
1410 event
.m_timeStamp
= ev
->when
;
1411 wxWindow
* focus
= wxWindow::FindFocus() ;
1412 event
.SetEventObject(focus
);
1414 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1416 event
.m_keyCode
= WXK_CONTROL
;
1417 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1418 focus
->GetEventHandler()->ProcessEvent( event
) ;
1420 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1422 event
.m_keyCode
= WXK_SHIFT
;
1423 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1424 focus
->GetEventHandler()->ProcessEvent( event
) ;
1426 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1428 event
.m_keyCode
= WXK_ALT
;
1429 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1430 focus
->GetEventHandler()->ProcessEvent( event
) ;
1432 if ( ( ev
->modifiers
^ s_lastModifiers
) & cmdKey
)
1434 event
.m_keyCode
= WXK_COMMAND
;
1435 event
.SetEventType( ( ev
->modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1436 focus
->GetEventHandler()->ProcessEvent( event
) ;
1438 s_lastModifiers
= ev
->modifiers
;
1442 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1444 // we must avoid reentrancy problems when processing high level events eg printing
1445 bool former
= s_inYield
;
1447 EventRecord
* ev
= (EventRecord
*) evr
;
1448 ::AEProcessAppleEvent( ev
) ;
1449 s_inYield
= former
;
1452 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1454 EventRecord
* ev
= (EventRecord
*) evr
;
1455 wxToolTip::RemoveToolTips() ;
1458 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1459 WindowAttributes frontWindowAttributes
= NULL
;
1461 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1463 short windowPart
= ::FindWindow(ev
->where
, &window
);
1464 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1465 if ( wxPendingDelete
.Member(win
) )
1469 GetQDGlobalsScreenBits( &screenBits
);
1474 if ( s_macIsInModalLoop
)
1480 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1481 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1482 s_lastMouseDown
= 0;
1486 SystemClick( ev
, window
) ;
1487 s_lastMouseDown
= 0;
1490 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1496 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1501 Point pt
= { 0, 0 } ;
1502 SetPortWindowPort(window
) ;
1503 LocalToGlobal( &pt
) ;
1505 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1506 -1 , wxSIZE_USE_EXISTING
);
1508 s_lastMouseDown
= 0;
1512 if (TrackGoAway(window
, ev
->where
))
1517 s_lastMouseDown
= 0;
1521 Rect newContentRect
;
1522 Rect constraintRect
;
1523 constraintRect
.top
= win
->GetMinHeight() ;
1524 if ( constraintRect
.top
== -1 )
1525 constraintRect
.top
= 0 ;
1526 constraintRect
.left
= win
->GetMinWidth() ;
1527 if ( constraintRect
.left
== -1 )
1528 constraintRect
.left
= 0 ;
1529 constraintRect
.right
= win
->GetMaxWidth() ;
1530 if ( constraintRect
.right
== -1 )
1531 constraintRect
.right
= 32000 ;
1532 constraintRect
.bottom
= win
->GetMaxHeight() ;
1533 if ( constraintRect
.bottom
== -1 )
1534 constraintRect
.bottom
= 32000 ;
1536 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1537 &constraintRect
, &newContentRect
) ;
1540 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1541 newContentRect
.right
- newContentRect
.left
,
1542 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1544 s_lastMouseDown
= 0;
1549 if (TrackBox(window
, ev
->where
, windowPart
))
1551 // TODO setup size event
1552 ZoomWindow( window
, windowPart
, false ) ;
1558 Point pt
= { 0, 0 } ;
1559 SetPortWindowPort(window
) ;
1560 LocalToGlobal( &pt
) ;
1563 GetWindowPortBounds(window
, &tempRect
) ;
1564 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1565 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1568 s_lastMouseDown
= 0;
1570 case inCollapseBox
:
1571 // TODO setup size event
1572 s_lastMouseDown
= 0;
1579 SetPortWindowPort(window
) ;
1582 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1584 if ( s_macIsInModalLoop
)
1588 else if ( UMAIsWindowFloating( window
) )
1591 win
->MacMouseDown( ev
, windowPart
) ;
1595 // Activate window first
1596 ::SelectWindow( window
) ;
1600 win
->MacMouseDown( ev
, windowPart
) ;
1606 win
->MacMouseDown( ev
, windowPart
) ;
1614 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1616 EventRecord
* ev
= (EventRecord
*) evr
;
1619 short windowPart
= inNoWindow
;
1620 if ( wxTheApp
->s_captureWindow
)
1622 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1623 windowPart
= inContent
;
1627 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1638 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1640 win
->MacMouseUp( ev
, windowPart
) ;
1648 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1649 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1654 case kHomeCharCode
:
1657 case kEnterCharCode
:
1658 retval
= WXK_RETURN
;
1663 case kHelpCharCode
:
1666 case kBackspaceCharCode
:
1672 case kPageUpCharCode
:
1673 retval
= WXK_PAGEUP
;
1675 case kPageDownCharCode
:
1676 retval
= WXK_PAGEDOWN
;
1678 case kReturnCharCode
:
1679 retval
= WXK_RETURN
;
1681 case kFunctionKeyCharCode
:
1733 case kEscapeCharCode
:
1734 retval
= WXK_ESCAPE
;
1736 case kLeftArrowCharCode
:
1739 case kRightArrowCharCode
:
1740 retval
= WXK_RIGHT
;
1742 case kUpArrowCharCode
:
1745 case kDownArrowCharCode
:
1748 case kDeleteCharCode
:
1749 retval
= WXK_DELETE
;
1758 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1760 EventRecord
* ev
= (EventRecord
*) evr
;
1761 wxToolTip::RemoveToolTips() ;
1763 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1764 if ( HiWord( menuresult
) )
1766 if ( !s_macIsInModalLoop
)
1767 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1771 wxWindow
* focus
= wxWindow::FindFocus() ;
1773 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1776 // we must handle control keys the other way round, otherwise text content is updated too late
1777 // has not been handled -> perform default
1778 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1779 if ( control
&& control
->GetMacControl() != NULL
)
1783 keychar
= short(ev
->message
& charCodeMask
);
1784 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1785 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1792 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1794 EventRecord
* ev
= (EventRecord
*) evr
;
1795 wxToolTip::RemoveToolTips() ;
1797 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1798 if ( HiWord( menuresult
) )
1803 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1809 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1816 keychar
= short(keymessage
& charCodeMask
);
1817 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1819 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1821 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1822 // and look at the character after
1824 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1825 keychar
= short(keyInfo
& charCodeMask
);
1826 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1828 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1829 long realkeyval
= keyval
;
1830 if ( keyval
== keychar
)
1832 // 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)
1833 realkeyval
= short(keymessage
& charCodeMask
) ;
1834 keyval
= wxToupper( keyval
) ;
1837 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1838 bool handled
= false ;
1839 event
.m_shiftDown
= modifiers
& shiftKey
;
1840 event
.m_controlDown
= modifiers
& controlKey
;
1841 event
.m_altDown
= modifiers
& optionKey
;
1842 event
.m_metaDown
= modifiers
& cmdKey
;
1843 event
.m_keyCode
= keyval
;
1847 event
.m_timeStamp
= when
;
1848 event
.SetEventObject(focus
);
1849 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1850 if ( handled
&& event
.GetSkipped() )
1857 wxWindow
*ancestor
= focus
;
1860 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1863 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1864 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1867 if (ancestor
->IsTopLevel())
1869 ancestor
= ancestor
->GetParent();
1872 #endif // wxUSE_ACCEL
1876 event
.Skip( FALSE
) ;
1877 event
.SetEventType( wxEVT_CHAR
) ;
1879 event
.m_keyCode
= realkeyval
;
1881 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1882 if ( handled
&& event
.GetSkipped() )
1886 (keyval
== WXK_TAB
) &&
1887 // CS: copied the change below from wxGTK
1888 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1889 // have this style, yet choose not to process this particular TAB in which
1890 // case TAB must still work as a navigational character
1892 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
1894 (focus
->GetParent()) &&
1895 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
1897 wxNavigationKeyEvent new_event
;
1898 new_event
.SetEventObject( focus
);
1899 new_event
.SetDirection( !event
.ShiftDown() );
1900 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1901 new_event
.SetWindowChange( event
.ControlDown() );
1902 new_event
.SetCurrentFocus( focus
);
1903 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1904 if ( handled
&& new_event
.GetSkipped() )
1907 // backdoor handler for default return and command escape
1908 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1910 // if window is not having a focus still testing for default enter or cancel
1911 // TODO add the UMA version for ActiveNonFloatingWindow
1912 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1915 if ( keyval
== WXK_RETURN
)
1917 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1919 if ( def
&& def
->IsEnabled() )
1921 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1922 event
.SetEventObject(def
);
1923 def
->Command(event
);
1927 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1928 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1930 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1931 new_event
.SetEventObject( focus
);
1932 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1939 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1946 keychar
= short(keymessage
& charCodeMask
);
1947 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1948 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1950 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1951 // and look at the character after
1953 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1954 keychar
= short(keyInfo
& charCodeMask
);
1955 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1957 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1959 if ( keyval
== keychar
)
1961 keyval
= wxToupper( keyval
) ;
1963 bool handled
= false ;
1965 wxKeyEvent
event(wxEVT_KEY_UP
);
1966 event
.m_shiftDown
= modifiers
& shiftKey
;
1967 event
.m_controlDown
= modifiers
& controlKey
;
1968 event
.m_altDown
= modifiers
& optionKey
;
1969 event
.m_metaDown
= modifiers
& cmdKey
;
1970 event
.m_keyCode
= keyval
;
1974 event
.m_timeStamp
= when
;
1975 event
.SetEventObject(focus
);
1976 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1982 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
1984 EventRecord
* ev
= (EventRecord
*) evr
;
1985 WindowRef window
= (WindowRef
) ev
->message
;
1988 bool activate
= (ev
->modifiers
& activeFlag
) ;
1989 WindowClass wclass
;
1990 ::GetWindowClass ( window
, &wclass
) ;
1991 if ( wclass
== kFloatingWindowClass
)
1993 // if it is a floater we activate/deactivate the front non-floating window instead
1994 window
= ::FrontNonFloatingWindow() ;
1996 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1998 win
->MacActivate( ev
->when
, activate
) ;
2002 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
2004 EventRecord
* ev
= (EventRecord
*) evr
;
2005 WindowRef window
= (WindowRef
) ev
->message
;
2006 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2009 if ( !wxPendingDelete
.Member(win
) )
2010 win
->MacUpdate( ev
->when
) ;
2014 // since there is no way of telling this foreign window to update itself
2015 // we have to invalidate the update region otherwise we keep getting the same
2016 // event over and over again
2017 BeginUpdate( window
) ;
2018 EndUpdate( window
) ;
2022 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
2024 EventRecord
* ev
= (EventRecord
*) evr
;
2025 if ( HiWord( ev
->message
) != noErr
)
2029 SetPt( &point
, 100 , 100 ) ;
2031 err
= DIBadMount( point
, ev
->message
) ;
2032 wxASSERT( err
== noErr
) ;
2036 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2038 EventRecord
* ev
= (EventRecord
*) evr
;
2039 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2041 case suspendResumeMessage
:
2043 bool isResuming
= ev
->message
& resumeFlag
;
2044 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2046 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2049 WindowRef oldFrontWindow
= NULL
;
2050 WindowRef newFrontWindow
= NULL
;
2052 // in case we don't take care of activating ourselves, we have to synchronize
2053 // our idea of the active window with the process manager's - which it already activated
2055 if ( !doesActivate
)
2056 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2058 MacResume( convertClipboard
) ;
2060 newFrontWindow
= ::FrontNonFloatingWindow() ;
2062 if ( oldFrontWindow
)
2064 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2066 win
->MacActivate( ev
->when
, false ) ;
2068 if ( newFrontWindow
)
2070 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2072 win
->MacActivate( ev
->when
, true ) ;
2077 MacSuspend( convertClipboard
) ;
2081 case mouseMovedMessage
:
2085 wxWindow
* currentMouseWindow
= NULL
;
2087 if (s_captureWindow
)
2089 currentMouseWindow
= s_captureWindow
;
2093 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2094 ¤tMouseWindow
) ;
2097 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2099 wxMouseEvent event
;
2101 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2102 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2104 event
.m_leftDown
= isDown
&& !controlDown
;
2105 event
.m_middleDown
= FALSE
;
2106 event
.m_rightDown
= isDown
&& controlDown
;
2107 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2108 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2109 event
.m_altDown
= ev
->modifiers
& optionKey
;
2110 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2111 event
.m_x
= ev
->where
.h
;
2112 event
.m_y
= ev
->where
.v
;
2113 event
.m_timeStamp
= ev
->when
;
2114 event
.SetEventObject(this);
2116 if ( wxWindow::s_lastMouseWindow
)
2118 wxMouseEvent
eventleave(event
);
2119 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2120 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2121 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2123 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2125 if ( currentMouseWindow
)
2127 wxMouseEvent
evententer(event
);
2128 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2129 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2130 evententer
.SetEventObject( currentMouseWindow
) ;
2131 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2133 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2136 short windowPart
= inNoWindow
;
2138 if ( s_captureWindow
)
2140 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2141 windowPart
= inContent
;
2145 windowPart
= ::FindWindow(ev
->where
, &window
);
2152 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2154 win
->MacMouseMoved( ev
, windowPart
) ;
2161 UMAShowArrowCursor();
2171 UMAShowArrowCursor();
2182 void wxApp::MacHandleMouseMovedEvent(wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
2186 wxWindow
* currentMouseWindow
= NULL
;
2188 if (s_captureWindow
)
2190 currentMouseWindow
= s_captureWindow
;
2194 wxWindow::MacGetWindowFromPoint( wxPoint( x
, y
) , ¤tMouseWindow
) ;
2197 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2199 wxMouseEvent event
;
2201 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
2202 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
2204 event
.m_leftDown
= isDown
&& !controlDown
;
2206 event
.m_middleDown
= FALSE
;
2207 event
.m_rightDown
= isDown
&& controlDown
;
2209 event
.m_shiftDown
= modifiers
& shiftKey
;
2210 event
.m_controlDown
= modifiers
& controlKey
;
2211 event
.m_altDown
= modifiers
& optionKey
;
2212 event
.m_metaDown
= modifiers
& cmdKey
;
2216 event
.m_timeStamp
= timestamp
;
2218 if ( wxWindow::s_lastMouseWindow
)
2220 wxMouseEvent
eventleave(event
);
2221 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2222 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2223 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2226 wxToolTip::RelayEvent( wxWindow::s_lastMouseWindow
, eventleave
);
2227 #endif // wxUSE_TOOLTIPS
2228 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2230 if ( currentMouseWindow
)
2232 wxMouseEvent
evententer(event
);
2233 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2234 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2235 evententer
.SetEventObject( currentMouseWindow
) ;
2237 wxToolTip::RelayEvent( currentMouseWindow
, evententer
);
2238 #endif // wxUSE_TOOLTIPS
2239 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2241 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2244 short windowPart
= inNoWindow
;
2246 if ( s_captureWindow
)
2248 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2249 windowPart
= inContent
;
2253 Point pt
= { y
, x
} ;
2254 windowPart
= ::FindWindow(pt
, &window
);
2261 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2263 win
->MacFireMouseEvent( nullEvent
, x
, y
, modifiers
, timestamp
) ;
2270 UMAShowArrowCursor();
2280 UMAShowArrowCursor();
2287 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2289 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2290 wxFrame
* frame
= mbar
->GetFrame();
2291 wxCHECK_RET( mbar
!= NULL
&& frame
!= NULL
, wxT("error in menu item callback") );
2294 frame
->ProcessCommand(id
);
2299 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2302 return; // no menu item selected
2304 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2307 Str255 deskAccessoryName
;
2310 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2311 GetPort(&savedPort
);
2312 OpenDeskAcc(deskAccessoryName
);
2319 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2320 MacHandleMenuCommand( id
) ;