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
) {
815 theResPath
= wxString(theLibPath
, wxConvLocal
);
817 theResPath
= wxString(theLibPath
);
819 // replace '_core' with '' in case of multi-lib build
820 theResPath
.Replace(wxT("_core"), wxEmptyString
);
821 // replace ".dylib" shared library extension with ".rsrc"
822 theResPath
.Replace(wxT(".dylib"), wxT(".rsrc"));
823 // Find the begining of the filename
824 theName
= theResPath
.AfterLast('/');
827 wxLogDebug( wxT("wxMac resources file name is '%s'"),
828 theResPath
.mb_str() );
831 theErr
= FSPathMakeRef((UInt8
*) theResPath
.mb_str(), &theResRef
, false);
832 if (theErr
!= noErr
) {
833 // try in current directory (using name only)
834 theErr
= FSPathMakeRef((UInt8
*) theName
.mb_str(), &theResRef
, false);
837 // open the resource file
838 if (theErr
== noErr
) {
839 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
840 &gSharedLibraryResource
);
842 if (theErr
!= noErr
) {
844 wxLogDebug( wxT("unable to open wxMac resource file '%s'\n"),
845 theResPath
.mb_str() );
846 #endif // __WXDEBUG__
851 #endif /* __DARWIN__ */
853 #endif /* WXMAKINGDLL_CORE */
856 void wxStAppResource::CloseSharedLibraryResource()
858 #ifdef WXMAKINGDLL_CORE
859 // Close the shared library resource file
860 if (gSharedLibraryResource
!= kResFileNotOpened
) {
862 if (gSharedLibraryBundle
!= NULL
) {
863 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
864 gSharedLibraryResource
);
865 gSharedLibraryBundle
= NULL
;
868 #endif /* __DARWIN__ */
870 CloseResFile(gSharedLibraryResource
);
872 gSharedLibraryResource
= kResFileNotOpened
;
874 #endif /* WXMAKINGDLL_CORE */
877 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
879 // for shared libraries we have to manually get the correct resource
880 // ref num upon initializing and releasing when terminating, therefore
881 // the __wxinitialize and __wxterminate must be used
884 void __sinit(void); /* (generated by linker) */
885 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
886 pascal void __terminate(void);
889 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
891 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
892 return __initialize( theInitBlock
) ;
895 pascal void __wxterminate(void)
897 wxStAppResource::CloseSharedLibraryResource() ;
901 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
905 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
907 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
908 OSStatus err
= noErr
;
911 switch( GetEventClass( event
) )
913 case kEventClassKeyboard
:
916 switch( GetEventKind(event
) )
918 case kEventRawKeyDown
:
919 rec
->what
= keyDown
;
921 case kEventRawKeyRepeat
:
922 rec
->what
= autoKey
;
924 case kEventRawKeyUp
:
927 case kEventRawKeyModifiersChanged
:
928 rec
->what
= nullEvent
;
937 unsigned char charCode
;
939 GetMouse( &rec
->where
) ;
941 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
942 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
943 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
944 rec
->modifiers
= modifiers
;
945 rec
->message
= (keyCode
<< 8 ) + charCode
;
949 case kEventClassTextInput
:
951 switch( GetEventKind( event
) )
953 case kEventTextInputUnicodeForKeyEvent
:
956 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
960 unsigned char charCode
;
962 GetMouse( &rec
->where
) ;
963 rec
->what
= keyDown
;
964 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
965 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
966 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
967 rec
->modifiers
= modifiers
;
968 rec
->message
= (keyCode
<< 8 ) + charCode
;
984 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
986 OSStatus result = eventNotHandledErr ;
989 switch ( GetEventClass( event ) )
991 case kEventClassKeyboard :
992 if ( wxMacConvertEventToRecord( event , &rec ) )
994 wxTheApp->MacHandleModifierEvents( &rec ) ;
995 wxTheApp->MacHandleOneEvent( &rec ) ;
999 case kEventClassTextInput :
1000 if ( wxMacConvertEventToRecord( event , &rec ) )
1002 wxTheApp->MacHandleModifierEvents( &rec ) ;
1003 wxTheApp->MacHandleOneEvent( &rec ) ;
1017 m_printMode
= wxPRINT_WINDOWS
;
1020 m_macCurrentEvent
= NULL
;
1022 m_macCurrentEventHandlerCallRef
= NULL
;
1026 int wxApp::MainLoop()
1038 void wxApp::ExitMainLoop()
1040 m_keepGoing
= FALSE
;
1043 // Is a message/event pending?
1044 bool wxApp::Pending()
1047 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1051 return EventAvail( everyEvent
, &event
) ;
1055 // Dispatch a message.
1056 bool wxApp::Dispatch()
1063 void wxApp::OnIdle(wxIdleEvent
& event
)
1065 wxAppBase::OnIdle(event
);
1067 // If they are pending events, we must process them: pending events are
1068 // either events to the threads other than main or events posted with
1069 // wxPostEvent() functions
1070 wxMacProcessNotifierAndPendingEvents();
1072 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1073 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1076 void wxApp::WakeUpIdle()
1087 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1090 GetTopWindow()->Close(TRUE
);
1093 // Default behaviour: close the application with prompts. The
1094 // user can veto the close, and therefore the end session.
1095 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1099 if (!GetTopWindow()->Close(!event
.CanVeto()))
1104 extern "C" void wxCYield() ;
1110 // Yield to other processes
1112 bool wxApp::Yield(bool onlyIfNeeded
)
1116 if ( !onlyIfNeeded
)
1118 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1127 YieldToAnyThread() ;
1129 // by definition yield should handle all non-processed events
1133 OSStatus status
= noErr
;
1136 s_inReceiveEvent
= true ;
1137 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1138 s_inReceiveEvent
= false ;
1140 if ( status
== eventLoopTimedOutErr
)
1142 // make sure next time the event loop will trigger idle events
1143 sleepTime
= kEventDurationNoWait
;
1145 else if ( status
== eventLoopQuitErr
)
1147 // according to QA1061 this may also occur when a WakeUp Process
1152 MacHandleOneEvent( theEvent
) ;
1153 ReleaseEvent(theEvent
);
1155 } while( status
== noErr
) ;
1159 // having a larger value here leads to large performance slowdowns
1160 // so we cannot give background apps more processor time here
1161 // we do so however having a large sleep value in the main event loop
1164 while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1166 MacHandleModifierEvents( &event
) ;
1167 MacHandleOneEvent( &event
);
1168 if ( event
.what
!= kHighLevelEvent
)
1169 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1171 MacHandleModifierEvents( &event
) ;
1174 wxMacProcessNotifierAndPendingEvents() ;
1180 // platform specifics
1182 void wxApp::MacSuspend( bool convertClipboard
)
1185 // we have to deactive the top level windows manually
1187 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1190 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1192 #if 0 // having problems right now with that
1193 if (!win
->HasFlag(wxSTAY_ON_TOP
))
1196 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1198 node
= node
->GetNext();
1201 ::HideFloatingWindows() ;
1203 s_lastMouseDown
= 0 ;
1205 if( convertClipboard
)
1207 MacConvertPrivateToPublicScrap() ;
1211 extern wxList wxModalDialogs
;
1213 void wxApp::MacResume( bool convertClipboard
)
1215 s_lastMouseDown
= 0 ;
1216 if( convertClipboard
)
1218 MacConvertPublicToPrivateScrap() ;
1222 ::ShowFloatingWindows() ;
1223 // raise modal dialogs in case a non modal window was selected to activate the app
1225 wxNode
* node
= wxModalDialogs
.GetFirst();
1228 wxDialog
* dialog
= (wxDialog
*) node
->GetData();
1231 node
= node
->GetNext();
1236 void wxApp::MacConvertPrivateToPublicScrap()
1240 void wxApp::MacConvertPublicToPrivateScrap()
1244 void wxApp::MacDoOneEvent()
1249 s_inReceiveEvent
= true ;
1250 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1251 s_inReceiveEvent
= false ;
1252 if ( status
== eventLoopTimedOutErr
)
1254 if ( wxTheApp
->ProcessIdle() )
1255 sleepTime
= kEventDurationNoWait
;
1258 if (g_numberOfThreads
)
1260 sleepTime
= kEventDurationNoWait
;
1264 sleepTime
= kEventDurationSecond
;
1268 else if ( status
== eventLoopQuitErr
)
1270 // according to QA1061 this may also occur when a WakeUp Process
1275 MacHandleOneEvent( theEvent
) ;
1276 ReleaseEvent(theEvent
);
1277 sleepTime
= kEventDurationNoWait
;
1282 EventMask eventMask
= everyEvent
;
1284 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1286 MacHandleModifierEvents( &event
) ;
1287 MacHandleOneEvent( &event
);
1291 MacHandleModifierEvents( &event
) ;
1293 WindowPtr window
= ::FrontWindow() ;
1295 ::IdleControls( window
) ;
1297 if ( wxTheApp
->ProcessIdle() )
1298 sleepTime
= kEventDurationNoWait
;
1301 if (g_numberOfThreads
)
1303 sleepTime
= kEventDurationNoWait
;
1307 sleepTime
= kEventDurationSecond
;
1311 if ( event
.what
!= kHighLevelEvent
)
1312 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1316 DeletePendingObjects() ;
1317 wxMacProcessNotifierAndPendingEvents() ;
1320 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1322 // Override to process unhandled events as you please
1325 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1328 EventTargetRef theTarget
;
1329 theTarget
= GetEventDispatcherTarget();
1330 m_macCurrentEvent
= evr
;
1331 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1332 if(status
== eventNotHandledErr
)
1334 MacHandleUnhandledEvent(evr
);
1337 EventRecord
* ev
= (EventRecord
*) evr
;
1338 m_macCurrentEvent
= ev
;
1340 wxApp::sm_lastMessageTime
= ev
->when
;
1345 MacHandleMouseDownEvent( ev
) ;
1346 if ( ev
->modifiers
& controlKey
)
1347 s_lastMouseDown
= 2;
1349 s_lastMouseDown
= 1;
1352 if ( s_lastMouseDown
== 2 )
1354 ev
->modifiers
|= controlKey
;
1358 ev
->modifiers
&= ~controlKey
;
1360 MacHandleMouseUpEvent( ev
) ;
1361 s_lastMouseDown
= 0;
1364 MacHandleActivateEvent( ev
) ;
1367 // In embedded mode we first let the UnhandledEvent function
1368 // try to handle the update event. If we handle it ourselves
1369 // first and then pass it on, the host's windows won't update.
1370 MacHandleUnhandledEvent(ev
);
1371 MacHandleUpdateEvent( ev
) ;
1375 MacHandleKeyDownEvent( ev
) ;
1378 MacHandleKeyUpEvent( ev
) ;
1381 MacHandleDiskEvent( ev
) ;
1384 MacHandleOSEvent( ev
) ;
1386 case kHighLevelEvent
:
1387 MacHandleHighLevelEvent( ev
) ;
1393 wxMacProcessNotifierAndPendingEvents() ;
1397 bool s_macIsInModalLoop
= false ;
1399 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1401 EventRecord
* ev
= (EventRecord
*) evr
;
1402 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1404 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1406 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1407 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1408 event
.m_altDown
= ev
->modifiers
& optionKey
;
1409 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1411 event
.m_x
= ev
->where
.h
;
1412 event
.m_y
= ev
->where
.v
;
1413 event
.m_timeStamp
= ev
->when
;
1414 wxWindow
* focus
= wxWindow::FindFocus() ;
1415 event
.SetEventObject(focus
);
1417 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1419 event
.m_keyCode
= WXK_CONTROL
;
1420 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1421 focus
->GetEventHandler()->ProcessEvent( event
) ;
1423 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1425 event
.m_keyCode
= WXK_SHIFT
;
1426 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1427 focus
->GetEventHandler()->ProcessEvent( event
) ;
1429 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1431 event
.m_keyCode
= WXK_ALT
;
1432 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1433 focus
->GetEventHandler()->ProcessEvent( event
) ;
1435 if ( ( ev
->modifiers
^ s_lastModifiers
) & cmdKey
)
1437 event
.m_keyCode
= WXK_COMMAND
;
1438 event
.SetEventType( ( ev
->modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1439 focus
->GetEventHandler()->ProcessEvent( event
) ;
1441 s_lastModifiers
= ev
->modifiers
;
1445 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1447 // we must avoid reentrancy problems when processing high level events eg printing
1448 bool former
= s_inYield
;
1450 EventRecord
* ev
= (EventRecord
*) evr
;
1451 ::AEProcessAppleEvent( ev
) ;
1452 s_inYield
= former
;
1455 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1457 EventRecord
* ev
= (EventRecord
*) evr
;
1458 wxToolTip::RemoveToolTips() ;
1461 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1462 WindowAttributes frontWindowAttributes
= NULL
;
1464 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1466 short windowPart
= ::FindWindow(ev
->where
, &window
);
1467 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1468 if ( wxPendingDelete
.Member(win
) )
1472 GetQDGlobalsScreenBits( &screenBits
);
1477 if ( s_macIsInModalLoop
)
1483 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1484 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1485 s_lastMouseDown
= 0;
1489 SystemClick( ev
, window
) ;
1490 s_lastMouseDown
= 0;
1493 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1499 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1504 Point pt
= { 0, 0 } ;
1505 SetPortWindowPort(window
) ;
1506 LocalToGlobal( &pt
) ;
1508 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1509 -1 , wxSIZE_USE_EXISTING
);
1511 s_lastMouseDown
= 0;
1515 if (TrackGoAway(window
, ev
->where
))
1520 s_lastMouseDown
= 0;
1524 Rect newContentRect
;
1525 Rect constraintRect
;
1526 constraintRect
.top
= win
->GetMinHeight() ;
1527 if ( constraintRect
.top
== -1 )
1528 constraintRect
.top
= 0 ;
1529 constraintRect
.left
= win
->GetMinWidth() ;
1530 if ( constraintRect
.left
== -1 )
1531 constraintRect
.left
= 0 ;
1532 constraintRect
.right
= win
->GetMaxWidth() ;
1533 if ( constraintRect
.right
== -1 )
1534 constraintRect
.right
= 32000 ;
1535 constraintRect
.bottom
= win
->GetMaxHeight() ;
1536 if ( constraintRect
.bottom
== -1 )
1537 constraintRect
.bottom
= 32000 ;
1539 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1540 &constraintRect
, &newContentRect
) ;
1543 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1544 newContentRect
.right
- newContentRect
.left
,
1545 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1547 s_lastMouseDown
= 0;
1552 if (TrackBox(window
, ev
->where
, windowPart
))
1554 // TODO setup size event
1555 ZoomWindow( window
, windowPart
, false ) ;
1561 Point pt
= { 0, 0 } ;
1562 SetPortWindowPort(window
) ;
1563 LocalToGlobal( &pt
) ;
1566 GetWindowPortBounds(window
, &tempRect
) ;
1567 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1568 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1571 s_lastMouseDown
= 0;
1573 case inCollapseBox
:
1574 // TODO setup size event
1575 s_lastMouseDown
= 0;
1582 SetPortWindowPort(window
) ;
1585 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1587 if ( s_macIsInModalLoop
)
1591 else if ( UMAIsWindowFloating( window
) )
1594 win
->MacMouseDown( ev
, windowPart
) ;
1598 // Activate window first
1599 ::SelectWindow( window
) ;
1603 win
->MacMouseDown( ev
, windowPart
) ;
1609 win
->MacMouseDown( ev
, windowPart
) ;
1617 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1619 EventRecord
* ev
= (EventRecord
*) evr
;
1622 short windowPart
= inNoWindow
;
1623 if ( wxTheApp
->s_captureWindow
)
1625 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1626 windowPart
= inContent
;
1630 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1641 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1643 win
->MacMouseUp( ev
, windowPart
) ;
1651 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1652 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1657 case kHomeCharCode
:
1660 case kEnterCharCode
:
1661 retval
= WXK_RETURN
;
1666 case kHelpCharCode
:
1669 case kBackspaceCharCode
:
1675 case kPageUpCharCode
:
1676 retval
= WXK_PAGEUP
;
1678 case kPageDownCharCode
:
1679 retval
= WXK_PAGEDOWN
;
1681 case kReturnCharCode
:
1682 retval
= WXK_RETURN
;
1684 case kFunctionKeyCharCode
:
1736 case kEscapeCharCode
:
1737 retval
= WXK_ESCAPE
;
1739 case kLeftArrowCharCode
:
1742 case kRightArrowCharCode
:
1743 retval
= WXK_RIGHT
;
1745 case kUpArrowCharCode
:
1748 case kDownArrowCharCode
:
1751 case kDeleteCharCode
:
1752 retval
= WXK_DELETE
;
1760 int wxKeyCodeToMacModifier(wxKeyCode key
)
1774 // return optionKey;
1784 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1786 // TODO: Have it use HID Manager on OSX...
1787 //if OS X > 10.2 (i.e. 10.2.x)
1788 //a known apple bug prevents the system from determining led
1789 //states with GetKeys... can only determine caps lock led
1790 return !!(GetCurrentKeyModifiers() & wxKeyCodeToMacModifier(key
));
1792 // KeyMapByteArray keymap;
1793 // GetKeys((BigEndianLong*)keymap);
1794 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1798 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1800 EventRecord
* ev
= (EventRecord
*) evr
;
1801 wxToolTip::RemoveToolTips() ;
1803 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1804 if ( HiWord( menuresult
) )
1806 if ( !s_macIsInModalLoop
)
1807 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1811 wxWindow
* focus
= wxWindow::FindFocus() ;
1813 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1816 // we must handle control keys the other way round, otherwise text content is updated too late
1817 // has not been handled -> perform default
1818 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1819 if ( control
&& control
->GetMacControl() != NULL
)
1823 keychar
= short(ev
->message
& charCodeMask
);
1824 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1825 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1832 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1834 EventRecord
* ev
= (EventRecord
*) evr
;
1835 wxToolTip::RemoveToolTips() ;
1837 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1838 if ( HiWord( menuresult
) )
1843 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1849 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1856 keychar
= short(keymessage
& charCodeMask
);
1857 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1859 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1861 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1862 // and look at the character after
1864 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1865 keychar
= short(keyInfo
& charCodeMask
);
1866 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1868 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1869 long realkeyval
= keyval
;
1870 if ( keyval
== keychar
)
1872 // 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)
1873 realkeyval
= short(keymessage
& charCodeMask
) ;
1874 keyval
= wxToupper( keyval
) ;
1877 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1878 bool handled
= false ;
1879 event
.m_shiftDown
= modifiers
& shiftKey
;
1880 event
.m_controlDown
= modifiers
& controlKey
;
1881 event
.m_altDown
= modifiers
& optionKey
;
1882 event
.m_metaDown
= modifiers
& cmdKey
;
1883 event
.m_keyCode
= keyval
;
1887 event
.m_timeStamp
= when
;
1888 event
.SetEventObject(focus
);
1889 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1890 if ( handled
&& event
.GetSkipped() )
1897 wxWindow
*ancestor
= focus
;
1900 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1903 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1904 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1907 if (ancestor
->IsTopLevel())
1909 ancestor
= ancestor
->GetParent();
1912 #endif // wxUSE_ACCEL
1916 event
.Skip( FALSE
) ;
1917 event
.SetEventType( wxEVT_CHAR
) ;
1919 event
.m_keyCode
= realkeyval
;
1921 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1922 if ( handled
&& event
.GetSkipped() )
1926 (keyval
== WXK_TAB
) &&
1927 // CS: copied the change below from wxGTK
1928 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1929 // have this style, yet choose not to process this particular TAB in which
1930 // case TAB must still work as a navigational character
1932 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
1934 (focus
->GetParent()) &&
1935 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
1937 wxNavigationKeyEvent new_event
;
1938 new_event
.SetEventObject( focus
);
1939 new_event
.SetDirection( !event
.ShiftDown() );
1940 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1941 new_event
.SetWindowChange( event
.ControlDown() );
1942 new_event
.SetCurrentFocus( focus
);
1943 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1944 if ( handled
&& new_event
.GetSkipped() )
1947 // backdoor handler for default return and command escape
1948 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1950 // if window is not having a focus still testing for default enter or cancel
1951 // TODO add the UMA version for ActiveNonFloatingWindow
1952 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1955 if ( keyval
== WXK_RETURN
)
1957 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1959 if ( def
&& def
->IsEnabled() )
1961 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1962 event
.SetEventObject(def
);
1963 def
->Command(event
);
1967 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1968 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1970 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1971 new_event
.SetEventObject( focus
);
1972 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1979 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1986 keychar
= short(keymessage
& charCodeMask
);
1987 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1988 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1990 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1991 // and look at the character after
1993 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1994 keychar
= short(keyInfo
& charCodeMask
);
1995 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1997 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1999 if ( keyval
== keychar
)
2001 keyval
= wxToupper( keyval
) ;
2003 bool handled
= false ;
2005 wxKeyEvent
event(wxEVT_KEY_UP
);
2006 event
.m_shiftDown
= modifiers
& shiftKey
;
2007 event
.m_controlDown
= modifiers
& controlKey
;
2008 event
.m_altDown
= modifiers
& optionKey
;
2009 event
.m_metaDown
= modifiers
& cmdKey
;
2010 event
.m_keyCode
= keyval
;
2014 event
.m_timeStamp
= when
;
2015 event
.SetEventObject(focus
);
2016 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2022 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
2024 EventRecord
* ev
= (EventRecord
*) evr
;
2025 WindowRef window
= (WindowRef
) ev
->message
;
2028 bool activate
= (ev
->modifiers
& activeFlag
) ;
2029 WindowClass wclass
;
2030 ::GetWindowClass ( window
, &wclass
) ;
2031 if ( wclass
== kFloatingWindowClass
)
2033 // if it is a floater we activate/deactivate the front non-floating window instead
2034 window
= ::FrontNonFloatingWindow() ;
2036 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2038 win
->MacActivate( ev
->when
, activate
) ;
2042 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
2044 EventRecord
* ev
= (EventRecord
*) evr
;
2045 WindowRef window
= (WindowRef
) ev
->message
;
2046 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2049 if ( !wxPendingDelete
.Member(win
) )
2050 win
->MacUpdate( ev
->when
) ;
2054 // since there is no way of telling this foreign window to update itself
2055 // we have to invalidate the update region otherwise we keep getting the same
2056 // event over and over again
2057 BeginUpdate( window
) ;
2058 EndUpdate( window
) ;
2062 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
2064 EventRecord
* ev
= (EventRecord
*) evr
;
2065 if ( HiWord( ev
->message
) != noErr
)
2069 SetPt( &point
, 100 , 100 ) ;
2071 err
= DIBadMount( point
, ev
->message
) ;
2072 wxASSERT( err
== noErr
) ;
2076 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2078 EventRecord
* ev
= (EventRecord
*) evr
;
2079 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2081 case suspendResumeMessage
:
2083 bool isResuming
= ev
->message
& resumeFlag
;
2084 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2086 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2089 WindowRef oldFrontWindow
= NULL
;
2090 WindowRef newFrontWindow
= NULL
;
2092 // in case we don't take care of activating ourselves, we have to synchronize
2093 // our idea of the active window with the process manager's - which it already activated
2095 if ( !doesActivate
)
2096 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2098 MacResume( convertClipboard
) ;
2100 newFrontWindow
= ::FrontNonFloatingWindow() ;
2102 if ( oldFrontWindow
)
2104 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2106 win
->MacActivate( ev
->when
, false ) ;
2108 if ( newFrontWindow
)
2110 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2112 win
->MacActivate( ev
->when
, true ) ;
2117 MacSuspend( convertClipboard
) ;
2121 case mouseMovedMessage
:
2125 wxWindow
* currentMouseWindow
= NULL
;
2127 if (s_captureWindow
)
2129 currentMouseWindow
= s_captureWindow
;
2133 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2134 ¤tMouseWindow
) ;
2137 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2139 wxMouseEvent event
;
2141 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2142 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2144 event
.m_leftDown
= isDown
&& !controlDown
;
2145 event
.m_middleDown
= FALSE
;
2146 event
.m_rightDown
= isDown
&& controlDown
;
2147 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2148 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2149 event
.m_altDown
= ev
->modifiers
& optionKey
;
2150 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2151 event
.m_x
= ev
->where
.h
;
2152 event
.m_y
= ev
->where
.v
;
2153 event
.m_timeStamp
= ev
->when
;
2154 event
.SetEventObject(this);
2156 if ( wxWindow::s_lastMouseWindow
)
2158 wxMouseEvent
eventleave(event
);
2159 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2160 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2161 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2163 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2165 if ( currentMouseWindow
)
2167 wxMouseEvent
evententer(event
);
2168 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2169 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2170 evententer
.SetEventObject( currentMouseWindow
) ;
2171 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2173 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2176 short windowPart
= inNoWindow
;
2178 if ( s_captureWindow
)
2180 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2181 windowPart
= inContent
;
2185 windowPart
= ::FindWindow(ev
->where
, &window
);
2192 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2194 win
->MacMouseMoved( ev
, windowPart
) ;
2201 UMAShowArrowCursor();
2211 UMAShowArrowCursor();
2222 void wxApp::MacHandleMouseMovedEvent(wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
2226 wxWindow
* currentMouseWindow
= NULL
;
2228 if (s_captureWindow
)
2230 currentMouseWindow
= s_captureWindow
;
2234 wxWindow::MacGetWindowFromPoint( wxPoint( x
, y
) , ¤tMouseWindow
) ;
2237 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2239 wxMouseEvent event
;
2241 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
2242 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
2244 event
.m_leftDown
= isDown
&& !controlDown
;
2246 event
.m_middleDown
= FALSE
;
2247 event
.m_rightDown
= isDown
&& controlDown
;
2249 event
.m_shiftDown
= modifiers
& shiftKey
;
2250 event
.m_controlDown
= modifiers
& controlKey
;
2251 event
.m_altDown
= modifiers
& optionKey
;
2252 event
.m_metaDown
= modifiers
& cmdKey
;
2256 event
.m_timeStamp
= timestamp
;
2258 if ( wxWindow::s_lastMouseWindow
)
2260 wxMouseEvent
eventleave(event
);
2261 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2262 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2263 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2266 wxToolTip::RelayEvent( wxWindow::s_lastMouseWindow
, eventleave
);
2267 #endif // wxUSE_TOOLTIPS
2268 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2270 if ( currentMouseWindow
)
2272 wxMouseEvent
evententer(event
);
2273 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2274 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2275 evententer
.SetEventObject( currentMouseWindow
) ;
2277 wxToolTip::RelayEvent( currentMouseWindow
, evententer
);
2278 #endif // wxUSE_TOOLTIPS
2279 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2281 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2284 short windowPart
= inNoWindow
;
2286 if ( s_captureWindow
)
2288 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2289 windowPart
= inContent
;
2293 Point pt
= { y
, x
} ;
2294 windowPart
= ::FindWindow(pt
, &window
);
2301 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2303 win
->MacFireMouseEvent( nullEvent
, x
, y
, modifiers
, timestamp
) ;
2310 UMAShowArrowCursor();
2320 UMAShowArrowCursor();
2327 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2329 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2330 wxFrame
* frame
= mbar
->GetFrame();
2331 wxCHECK_RET( mbar
!= NULL
&& frame
!= NULL
, wxT("error in menu item callback") );
2334 frame
->ProcessCommand(id
);
2339 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2342 return; // no menu item selected
2344 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2347 Str255 deskAccessoryName
;
2350 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2351 GetPort(&savedPort
);
2352 OpenDeskAcc(deskAccessoryName
);
2359 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2360 MacHandleMenuCommand( id
) ;