1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "app.h"
18 #include "wx/window.h"
20 #include "wx/button.h"
23 #include "wx/gdicmn.h"
26 #include "wx/cursor.h"
29 #include "wx/palette.h"
31 #include "wx/dialog.h"
32 #include "wx/msgdlg.h"
34 #include "wx/module.h"
35 #include "wx/memory.h"
36 #include "wx/tooltip.h"
37 #include "wx/textctrl.h"
39 #include "wx/docview.h"
53 #include "wx/mac/uma.h"
54 #include "wx/mac/macnotfy.h"
57 # include <CoreServices/CoreServices.h>
58 # if defined(WXMAKINGDLL_CORE)
59 # include <mach-o/dyld.h>
64 # include <ToolUtils.h>
65 # include <DiskInit.h>
69 extern wxList wxPendingDelete
;
70 extern wxList
*wxWinMacWindowList
;
71 extern wxList
*wxWinMacControlList
;
73 extern size_t g_numberOfThreads
;
74 #endif // wxUSE_THREADS
76 // statics for implementation
78 static bool s_inYield
= FALSE
;
81 static bool s_inReceiveEvent
= FALSE
;
82 static EventTime sleepTime
= kEventDurationNoWait
;
84 static long sleepTime
= 0 ;
87 #if !USE_SHARED_LIBRARY
88 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
89 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
90 EVT_IDLE(wxApp::OnIdle
)
91 EVT_END_SESSION(wxApp::OnEndSession
)
92 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
97 const short kMacMinHeap
= (29 * 1024) ;
98 // platform specific static variables
100 const short kwxMacMenuBarResource
= 1 ;
101 const short kwxMacAppleMenuId
= 1 ;
103 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
104 wxWindow
* wxApp::s_captureWindow
= NULL
;
105 int wxApp::s_lastMouseDown
= 0 ;
106 long wxApp::sm_lastMessageTime
= 0;
107 long wxApp::s_lastModifiers
= 0 ;
110 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
111 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
112 long wxApp::s_macPreferencesMenuItemId
= wxID_PREFERENCES
;
113 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
114 wxString
wxApp::s_macHelpMenuTitleName
= wxT("&Help") ;
116 // Normally we're not a plugin
117 bool wxApp::sm_isEmbedded
= false;
118 //----------------------------------------------------------------------
119 // Core Apple Event Support
120 //----------------------------------------------------------------------
122 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
123 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
124 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
125 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
127 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
129 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
132 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
134 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
137 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
139 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
142 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
144 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
147 // AEODoc Calls MacOpenFile on each of the files passed
149 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
153 DescType returnedType
;
159 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
163 err
= AECountItems(&docList
, &itemsInList
);
167 ProcessSerialNumber PSN
;
168 PSN
.highLongOfPSN
= 0 ;
169 PSN
.lowLongOfPSN
= kCurrentProcess
;
170 SetFrontProcess( &PSN
) ;
172 for (i
= 1; i
<= itemsInList
; i
++) {
173 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
174 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
175 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
181 // AEPDoc Calls MacPrintFile on each of the files passed
183 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
187 DescType returnedType
;
193 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
197 err
= AECountItems(&docList
, &itemsInList
);
201 ProcessSerialNumber PSN
;
202 PSN
.highLongOfPSN
= 0 ;
203 PSN
.lowLongOfPSN
= kCurrentProcess
;
204 SetFrontProcess( &PSN
) ;
206 for (i
= 1; i
<= itemsInList
; i
++) {
207 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
208 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
209 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
215 // AEOApp calls MacNewFile
217 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
223 // AEQuit attempts to quit the application
225 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
227 wxWindow
* win
= GetTopWindow() ;
239 //----------------------------------------------------------------------
240 // Support Routines linking the Mac...File Calls to the Document Manager
241 //----------------------------------------------------------------------
243 void wxApp::MacOpenFile(const wxString
& fileName
)
245 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
247 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
250 void wxApp::MacPrintFile(const wxString
& fileName
)
252 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
255 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
258 wxView
* view
= doc
->GetFirstView() ;
261 wxPrintout
*printout
= view
->OnCreatePrintout();
265 printer
.Print(view
->GetFrame(), printout
, TRUE
);
271 doc
->DeleteAllViews();
272 dm
->RemoveDocument(doc
) ;
278 void wxApp::MacNewFile()
282 //----------------------------------------------------------------------
283 // Carbon Event Handler
284 //----------------------------------------------------------------------
288 static const EventTypeSpec eventList
[] =
290 { kEventClassCommand
, kEventProcessCommand
} ,
291 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
293 { kEventClassMenu
, kEventMenuOpening
},
294 { kEventClassMenu
, kEventMenuClosed
},
295 { kEventClassMenu
, kEventMenuTargetItem
},
297 { kEventClassApplication
, kEventAppActivated
} ,
298 { kEventClassApplication
, kEventAppDeactivated
} ,
299 // handling the quit event is not recommended by apple
300 // rather using the quit apple event - which we do
302 { kEventClassAppleEvent
, kEventAppleEvent
} ,
304 { kEventClassMouse
, kEventMouseDown
} ,
305 { kEventClassMouse
, kEventMouseMoved
} ,
306 { kEventClassMouse
, kEventMouseUp
} ,
307 { kEventClassMouse
, kEventMouseDragged
} ,
311 static pascal OSStatus
312 MenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
314 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar();
315 wxFrame
* win
= mbar
->GetFrame();
319 // VZ: we could find the menu from its handle here by examining all
320 // the menus in the menu bar recursively but knowing that neither
321 // wxMSW nor wxGTK do it why bother...
325 GetEventParameter(event
,
326 kEventParamDirectObject
,
328 sizeof(menuRef
), NULL
,
334 switch (GetEventKind(event
))
336 case kEventMenuOpening
:
337 type
= wxEVT_MENU_OPEN
;
339 case kEventMenuClosed
:
340 type
= wxEVT_MENU_CLOSE
;
342 case kEventMenuTargetItem
:
343 type
= wxEVT_MENU_HIGHLIGHT
;
344 GetEventParameter(event
, kEventParamMenuCommand
,
345 typeMenuCommand
, NULL
,
346 sizeof(cmd
), NULL
, &cmd
);
347 if (cmd
== 0) return eventNotHandledErr
;
350 wxFAIL_MSG(wxT("Unexpected menu event kind"));
354 wxMenuEvent
wxevent(type
, cmd
);
355 wxevent
.SetEventObject(win
);
357 (void)win
->GetEventHandler()->ProcessEvent(wxevent
);
360 return eventNotHandledErr
;
363 // due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop
364 // but have to use ReceiveNextEvent dealing with events manually, therefore we also have
365 // deal with clicks in the menu bar explicitely
367 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
) ;
369 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
371 OSStatus result
= eventNotHandledErr
;
374 UInt32 modifiers
= 0;
375 EventMouseButton button
= 0 ;
378 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
379 sizeof( Point
), NULL
, &point
);
380 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
,
381 sizeof( UInt32
), NULL
, &modifiers
);
382 GetEventParameter( event
, kEventParamMouseButton
, typeMouseButton
, NULL
,
383 sizeof( EventMouseButton
), NULL
, &button
);
384 GetEventParameter( event
, kEventParamClickCount
, typeUInt32
, NULL
,
385 sizeof( UInt32
), NULL
, &click
);
387 if ( button
== 0 || GetEventKind( event
) == kEventMouseUp
)
388 modifiers
+= btnState
;
391 switch( GetEventKind(event
) )
393 case kEventMouseDown
:
397 short windowPart
= ::FindWindow(point
, &window
);
399 if ( windowPart
== inMenuBar
)
401 MenuSelect( point
) ;
406 case kEventMouseDragged
:
409 if ( wxTheApp
->s_captureWindow
)
410 wxMacWindowEventHandler( handler
, event
, (void*) wxTheApp
->s_captureWindow
->MacGetTopLevelWindow() ) ;
413 case kEventMouseMoved
:
415 wxTheApp
->MacHandleMouseMovedEvent( point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
425 static pascal OSStatus
CommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
427 OSStatus result
= eventNotHandledErr
;
431 GetEventParameter( event
, kEventParamDirectObject
, typeHICommand
, NULL
,
432 sizeof( HICommand
), NULL
, &command
);
434 MenuCommand id
= command
.commandID
;
435 if ( id
== kHICommandPreferences
)
436 id
= wxApp::s_macPreferencesMenuItemId
;
438 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
439 wxMenu
* menu
= NULL
;
440 wxMenuItem
* item
= NULL
;
444 item
= mbar
->FindItem( id
, &menu
) ;
445 // it is not 100 % sure that an menu of id 0 is really ours, safety check
446 if ( id
== 0 && menu
!= NULL
&& menu
->GetHMenu() != command
.menu
.menuRef
)
453 if ( item
== NULL
|| menu
== NULL
|| mbar
== NULL
)
456 switch( GetEventKind( event
) )
458 case kEventProcessCommand
:
460 if (item
->IsCheckable())
462 item
->Check( !item
->IsChecked() ) ;
465 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
469 case kEventCommandUpdateStatus
:
470 // eventually trigger an updateui round
480 static pascal OSStatus
ApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
482 OSStatus result
= eventNotHandledErr
;
483 switch ( GetEventKind( event
) )
485 case kEventAppActivated
:
488 wxTheApp
->MacResume( true ) ;
492 case kEventAppDeactivated
:
495 wxTheApp
->MacSuspend( true ) ;
505 pascal OSStatus
wxAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
507 OSStatus result
= eventNotHandledErr
;
508 switch( GetEventClass( event
) )
510 case kEventClassCommand
:
511 result
= CommandEventHandler( handler
, event
, data
) ;
513 case kEventClassApplication
:
514 result
= ApplicationEventHandler( handler
, event
, data
) ;
516 case kEventClassMenu
:
517 result
= MenuEventHandler( handler
, event
, data
) ;
519 case kEventClassMouse
:
520 result
= MouseEventHandler( handler
, event
, data
) ;
522 case kEventClassAppleEvent
:
525 wxMacConvertEventToRecord( event
, &rec
) ;
526 result
= AEProcessAppleEvent( &rec
) ;
536 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler
)
540 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
541 // we know it's there ;-)
542 WXIMPORT
char std::__throws_bad_alloc
;
545 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
551 UMAInitToolbox( 4, sm_isEmbedded
) ;
552 SetEventMask( everyEvent
) ;
553 UMAShowWatchCursor() ;
555 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
556 // open shared library resources from here since we don't have
557 // __wxinitialize in Mach-O shared libraries
558 wxStAppResource::OpenSharedLibraryResource(NULL
);
562 // test the minimal configuration necessary
568 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
570 error
= kMacSTRWrongMachine
;
572 else if (theMachine
< gestaltMacPlus
)
574 error
= kMacSTRWrongMachine
;
576 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
578 error
= kMacSTROldSystem
;
580 else if ( theSystem
< 0x0860 )
582 error
= kMacSTROldSystem
;
584 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
586 error
= kMacSTRSmallSize
;
592 if ( !UMAHasAppearance() )
594 error = kMacSTRNoPre8Yet ;
600 // if we encountered any problems so far, give the error code and exit immediately
604 wxStAppResource resload
;
608 GetIndString(message
, 128, error
);
609 UMAShowArrowCursor() ;
610 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
611 itemHit
= Alert(128, nil
);
616 # if __option(profile)
617 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
622 // now avoid exceptions thrown for new (bad_alloc)
623 // FIXME CS for some changes outside wxMac does not compile anymore
625 std::__throws_bad_alloc
= 0 ;
630 s_macCursorRgn
= ::NewRgn() ;
632 // Mac OS X passes a process serial number command line argument when
633 // the application is launched from the Finder. This argument must be
634 // removed from the command line arguments before being handled by the
635 // application (otherwise applications would need to handle it)
638 static const wxChar
*ARG_PSN
= _T("-psn_");
639 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
641 // remove this argument
643 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
647 if ( !wxAppBase::Initialize(argc
, argv
) )
651 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
655 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
656 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
658 wxMacCreateNotifierTable() ;
660 UMAShowArrowCursor() ;
665 bool wxApp::OnInitGui()
667 if( !wxAppBase::OnInitGui() )
671 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
675 InstallApplicationEventHandler(
676 GetwxAppEventHandlerUPP(),
677 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
683 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
684 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
685 NewAEEventHandlerUPP(AEHandleODoc
) ,
687 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
688 NewAEEventHandlerUPP(AEHandleOApp
) ,
690 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
691 NewAEEventHandlerUPP(AEHandlePDoc
) ,
693 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
694 NewAEEventHandlerUPP(AEHandleQuit
) ,
697 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
698 NewAEEventHandlerProc(AEHandleODoc
) ,
700 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
701 NewAEEventHandlerProc(AEHandleOApp
) ,
703 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
704 NewAEEventHandlerProc(AEHandlePDoc
) ,
706 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
707 NewAEEventHandlerProc(AEHandleQuit
) ,
715 void wxApp::CleanUp()
717 wxToolTip::RemoveToolTips() ;
719 // One last chance for pending objects to be cleaned up
720 wxTheApp
->DeletePendingObjects();
722 wxMacDestroyNotifierTable() ;
724 delete wxWinMacWindowList
;
725 wxWinMacWindowList
= NULL
;
727 delete wxWinMacControlList
;
728 wxWinMacControlList
= NULL
;
731 # if __option(profile)
732 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
737 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
738 // close shared library resources from here since we don't have
739 // __wxterminate in Mach-O shared libraries
740 wxStAppResource::CloseSharedLibraryResource();
743 UMACleanupToolbox() ;
744 if (s_macCursorRgn
) {
745 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
752 wxAppBase::CleanUp();
755 //----------------------------------------------------------------------
756 // misc initialization stuff
757 //----------------------------------------------------------------------
759 // extern variable for shared library resource id
760 // need to be able to find it with NSLookupAndBindSymbol
761 short gSharedLibraryResource
= kResFileNotOpened
;
763 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
764 CFBundleRef gSharedLibraryBundle
= NULL
;
765 #endif /* WXMAKINGDLL_CORE && __DARWIN__ */
767 wxStAppResource::wxStAppResource()
769 m_currentRefNum
= CurResFile() ;
770 if ( gSharedLibraryResource
!= kResFileNotOpened
)
772 UseResFile( gSharedLibraryResource
) ;
776 wxStAppResource::~wxStAppResource()
778 if ( m_currentRefNum
!= kResFileNotOpened
)
780 UseResFile( m_currentRefNum
) ;
784 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
786 gSharedLibraryResource
= kResFileNotOpened
;
788 #ifdef WXMAKINGDLL_CORE
789 if ( initBlock
!= NULL
) {
790 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
791 FSSpec
*fileSpec
= NULL
;
793 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
794 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
796 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
797 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
800 if (fileSpec
!= NULL
) {
801 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
806 // Open the shared library resource file if it is not yet open
809 const char *theLibPath
;
811 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
812 if (gSharedLibraryBundle
!= NULL
) {
813 // wxWindows has been bundled into a framework
814 // load the framework resources
816 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
819 // wxWindows is a simple dynamic shared library
820 // load the resources from the data fork of a separate resource file
824 OSErr theErr
= noErr
;
826 // get the library path
827 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
828 theModule
= NSModuleForSymbol(theSymbol
);
829 theLibPath
= NSLibraryNameForModule(theModule
);
831 // if we call wxLogDebug from here then, as wxTheApp hasn't been
832 // created yet when we're called from wxApp::Initialize(), wxLog
833 // is going to create a default stderr-based log target instead of
834 // the expected normal GUI one -- don't do it, if we really want
835 // to see this message just use fprintf() here
837 wxLogDebug( wxT("wxMac library installation name is '%s'"),
841 // allocate copy to replace .dylib.* extension with .rsrc
842 if (theLibPath
!= NULL
) {
844 theResPath
= wxString(theLibPath
, wxConvLocal
);
846 theResPath
= wxString(theLibPath
);
848 // replace '_core' with '' in case of multi-lib build
849 theResPath
.Replace(wxT("_core"), wxEmptyString
);
850 // replace ".dylib" shared library extension with ".rsrc"
851 theResPath
.Replace(wxT(".dylib"), wxT(".rsrc"));
852 // Find the begining of the filename
853 theName
= theResPath
.AfterLast('/');
856 wxLogDebug( wxT("wxMac resources file name is '%s'"),
857 theResPath
.mb_str() );
860 theErr
= FSPathMakeRef((UInt8
*) theResPath
.mb_str(), &theResRef
, false);
861 if (theErr
!= noErr
) {
862 // try in current directory (using name only)
863 theErr
= FSPathMakeRef((UInt8
*) theName
.mb_str(), &theResRef
, false);
866 // open the resource file
867 if (theErr
== noErr
) {
868 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
869 &gSharedLibraryResource
);
871 if (theErr
!= noErr
) {
873 wxLogDebug( wxT("unable to open wxMac resource file '%s'\n"),
874 theResPath
.mb_str() );
875 #endif // __WXDEBUG__
880 #endif /* __DARWIN__ */
882 #endif /* WXMAKINGDLL_CORE */
885 void wxStAppResource::CloseSharedLibraryResource()
887 #ifdef WXMAKINGDLL_CORE
888 // Close the shared library resource file
889 if (gSharedLibraryResource
!= kResFileNotOpened
) {
891 if (gSharedLibraryBundle
!= NULL
) {
892 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
893 gSharedLibraryResource
);
894 gSharedLibraryBundle
= NULL
;
897 #endif /* __DARWIN__ */
899 CloseResFile(gSharedLibraryResource
);
901 gSharedLibraryResource
= kResFileNotOpened
;
903 #endif /* WXMAKINGDLL_CORE */
906 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
908 // for shared libraries we have to manually get the correct resource
909 // ref num upon initializing and releasing when terminating, therefore
910 // the __wxinitialize and __wxterminate must be used
913 void __sinit(void); /* (generated by linker) */
914 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
915 pascal void __terminate(void);
918 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
920 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
921 return __initialize( theInitBlock
) ;
924 pascal void __wxterminate(void)
926 wxStAppResource::CloseSharedLibraryResource() ;
930 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
934 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
936 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
937 OSStatus err
= noErr
;
940 switch( GetEventClass( event
) )
942 case kEventClassKeyboard
:
945 switch( GetEventKind(event
) )
947 case kEventRawKeyDown
:
948 rec
->what
= keyDown
;
950 case kEventRawKeyRepeat
:
951 rec
->what
= autoKey
;
953 case kEventRawKeyUp
:
956 case kEventRawKeyModifiersChanged
:
957 rec
->what
= nullEvent
;
966 unsigned char charCode
;
968 GetMouse( &rec
->where
) ;
970 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
971 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
972 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
973 rec
->modifiers
= modifiers
;
974 rec
->message
= (keyCode
<< 8 ) + charCode
;
978 case kEventClassTextInput
:
980 switch( GetEventKind( event
) )
982 case kEventTextInputUnicodeForKeyEvent
:
985 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
989 unsigned char charCode
;
991 GetMouse( &rec
->where
) ;
992 rec
->what
= keyDown
;
993 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
994 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
995 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
996 rec
->modifiers
= modifiers
;
997 rec
->message
= (keyCode
<< 8 ) + charCode
;
1013 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1015 OSStatus result = eventNotHandledErr ;
1018 switch ( GetEventClass( event ) )
1020 case kEventClassKeyboard :
1021 if ( wxMacConvertEventToRecord( event , &rec ) )
1023 wxTheApp->MacHandleModifierEvents( &rec ) ;
1024 wxTheApp->MacHandleOneEvent( &rec ) ;
1028 case kEventClassTextInput :
1029 if ( wxMacConvertEventToRecord( event , &rec ) )
1031 wxTheApp->MacHandleModifierEvents( &rec ) ;
1032 wxTheApp->MacHandleOneEvent( &rec ) ;
1046 m_printMode
= wxPRINT_WINDOWS
;
1049 m_macCurrentEvent
= NULL
;
1051 m_macCurrentEventHandlerCallRef
= NULL
;
1055 int wxApp::MainLoop()
1067 void wxApp::ExitMainLoop()
1069 m_keepGoing
= FALSE
;
1072 // Is a message/event pending?
1073 bool wxApp::Pending()
1076 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1080 return EventAvail( everyEvent
, &event
) ;
1084 // Dispatch a message.
1085 bool wxApp::Dispatch()
1092 void wxApp::OnIdle(wxIdleEvent
& event
)
1094 wxAppBase::OnIdle(event
);
1096 // If they are pending events, we must process them: pending events are
1097 // either events to the threads other than main or events posted with
1098 // wxPostEvent() functions
1099 wxMacProcessNotifierAndPendingEvents();
1101 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1102 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1105 void wxApp::WakeUpIdle()
1116 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1119 GetTopWindow()->Close(TRUE
);
1122 // Default behaviour: close the application with prompts. The
1123 // user can veto the close, and therefore the end session.
1124 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1128 if (!GetTopWindow()->Close(!event
.CanVeto()))
1133 extern "C" void wxCYield() ;
1139 // Yield to other processes
1141 bool wxApp::Yield(bool onlyIfNeeded
)
1145 if ( !onlyIfNeeded
)
1147 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1156 YieldToAnyThread() ;
1158 // by definition yield should handle all non-processed events
1162 OSStatus status
= noErr
;
1165 s_inReceiveEvent
= true ;
1166 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1167 s_inReceiveEvent
= false ;
1169 if ( status
== eventLoopTimedOutErr
)
1171 // make sure next time the event loop will trigger idle events
1172 sleepTime
= kEventDurationNoWait
;
1174 else if ( status
== eventLoopQuitErr
)
1176 // according to QA1061 this may also occur when a WakeUp Process
1181 MacHandleOneEvent( theEvent
) ;
1182 ReleaseEvent(theEvent
);
1184 } while( status
== noErr
) ;
1188 // having a larger value here leads to large performance slowdowns
1189 // so we cannot give background apps more processor time here
1190 // we do so however having a large sleep value in the main event loop
1193 while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1195 MacHandleModifierEvents( &event
) ;
1196 MacHandleOneEvent( &event
);
1197 if ( event
.what
!= kHighLevelEvent
)
1198 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1200 MacHandleModifierEvents( &event
) ;
1203 wxMacProcessNotifierAndPendingEvents() ;
1209 // platform specifics
1211 void wxApp::MacSuspend( bool convertClipboard
)
1214 // we have to deactive the top level windows manually
1216 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1219 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1221 #if 0 // having problems right now with that
1222 if (!win
->HasFlag(wxSTAY_ON_TOP
))
1225 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1227 node
= node
->GetNext();
1230 ::HideFloatingWindows() ;
1232 s_lastMouseDown
= 0 ;
1234 if( convertClipboard
)
1236 MacConvertPrivateToPublicScrap() ;
1240 extern wxList wxModalDialogs
;
1242 void wxApp::MacResume( bool convertClipboard
)
1244 s_lastMouseDown
= 0 ;
1245 if( convertClipboard
)
1247 MacConvertPublicToPrivateScrap() ;
1251 ::ShowFloatingWindows() ;
1252 // raise modal dialogs in case a non modal window was selected to activate the app
1254 wxNode
* node
= wxModalDialogs
.GetFirst();
1257 wxDialog
* dialog
= (wxDialog
*) node
->GetData();
1260 node
= node
->GetNext();
1265 void wxApp::MacConvertPrivateToPublicScrap()
1269 void wxApp::MacConvertPublicToPrivateScrap()
1273 void wxApp::MacDoOneEvent()
1278 s_inReceiveEvent
= true ;
1279 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1280 s_inReceiveEvent
= false ;
1281 if ( status
== eventLoopTimedOutErr
)
1283 if ( wxTheApp
->ProcessIdle() )
1284 sleepTime
= kEventDurationNoWait
;
1288 if (g_numberOfThreads
)
1290 sleepTime
= kEventDurationNoWait
;
1293 #endif // wxUSE_THREADS
1295 sleepTime
= kEventDurationSecond
;
1299 else if ( status
== eventLoopQuitErr
)
1301 // according to QA1061 this may also occur when a WakeUp Process
1306 MacHandleOneEvent( theEvent
) ;
1307 ReleaseEvent(theEvent
);
1308 sleepTime
= kEventDurationNoWait
;
1313 EventMask eventMask
= everyEvent
;
1315 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1317 MacHandleModifierEvents( &event
) ;
1318 MacHandleOneEvent( &event
);
1322 MacHandleModifierEvents( &event
) ;
1324 WindowPtr window
= ::FrontWindow() ;
1326 ::IdleControls( window
) ;
1328 if ( wxTheApp
->ProcessIdle() )
1329 sleepTime
= kEventDurationNoWait
;
1333 if (g_numberOfThreads
)
1335 sleepTime
= kEventDurationNoWait
;
1338 #endif // wxUSE_THREADS
1340 sleepTime
= kEventDurationSecond
;
1344 if ( event
.what
!= kHighLevelEvent
)
1345 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1349 DeletePendingObjects() ;
1350 wxMacProcessNotifierAndPendingEvents() ;
1353 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1355 // Override to process unhandled events as you please
1358 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1361 EventTargetRef theTarget
;
1362 theTarget
= GetEventDispatcherTarget();
1363 m_macCurrentEvent
= evr
;
1364 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1365 if(status
== eventNotHandledErr
)
1367 MacHandleUnhandledEvent(evr
);
1370 EventRecord
* ev
= (EventRecord
*) evr
;
1371 m_macCurrentEvent
= ev
;
1373 wxApp::sm_lastMessageTime
= ev
->when
;
1378 MacHandleMouseDownEvent( ev
) ;
1379 if ( ev
->modifiers
& controlKey
)
1380 s_lastMouseDown
= 2;
1382 s_lastMouseDown
= 1;
1385 if ( s_lastMouseDown
== 2 )
1387 ev
->modifiers
|= controlKey
;
1391 ev
->modifiers
&= ~controlKey
;
1393 MacHandleMouseUpEvent( ev
) ;
1394 s_lastMouseDown
= 0;
1397 MacHandleActivateEvent( ev
) ;
1400 // In embedded mode we first let the UnhandledEvent function
1401 // try to handle the update event. If we handle it ourselves
1402 // first and then pass it on, the host's windows won't update.
1403 MacHandleUnhandledEvent(ev
);
1404 MacHandleUpdateEvent( ev
) ;
1408 MacHandleKeyDownEvent( ev
) ;
1411 MacHandleKeyUpEvent( ev
) ;
1414 MacHandleDiskEvent( ev
) ;
1417 MacHandleOSEvent( ev
) ;
1419 case kHighLevelEvent
:
1420 MacHandleHighLevelEvent( ev
) ;
1426 wxMacProcessNotifierAndPendingEvents() ;
1430 bool s_macIsInModalLoop
= false ;
1432 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1434 EventRecord
* ev
= (EventRecord
*) evr
;
1435 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1437 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1439 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1440 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1441 event
.m_altDown
= ev
->modifiers
& optionKey
;
1442 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1444 event
.m_x
= ev
->where
.h
;
1445 event
.m_y
= ev
->where
.v
;
1446 event
.m_timeStamp
= ev
->when
;
1447 wxWindow
* focus
= wxWindow::FindFocus() ;
1448 event
.SetEventObject(focus
);
1450 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1452 event
.m_keyCode
= WXK_CONTROL
;
1453 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1454 focus
->GetEventHandler()->ProcessEvent( event
) ;
1456 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1458 event
.m_keyCode
= WXK_SHIFT
;
1459 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1460 focus
->GetEventHandler()->ProcessEvent( event
) ;
1462 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1464 event
.m_keyCode
= WXK_ALT
;
1465 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1466 focus
->GetEventHandler()->ProcessEvent( event
) ;
1468 if ( ( ev
->modifiers
^ s_lastModifiers
) & cmdKey
)
1470 event
.m_keyCode
= WXK_COMMAND
;
1471 event
.SetEventType( ( ev
->modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1472 focus
->GetEventHandler()->ProcessEvent( event
) ;
1474 s_lastModifiers
= ev
->modifiers
;
1478 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1480 // we must avoid reentrancy problems when processing high level events eg printing
1481 bool former
= s_inYield
;
1483 EventRecord
* ev
= (EventRecord
*) evr
;
1484 ::AEProcessAppleEvent( ev
) ;
1485 s_inYield
= former
;
1488 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1490 EventRecord
* ev
= (EventRecord
*) evr
;
1491 wxToolTip::RemoveToolTips() ;
1494 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1495 WindowAttributes frontWindowAttributes
= NULL
;
1497 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1499 short windowPart
= ::FindWindow(ev
->where
, &window
);
1500 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1501 if ( wxPendingDelete
.Member(win
) )
1505 GetQDGlobalsScreenBits( &screenBits
);
1510 if ( s_macIsInModalLoop
)
1516 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1517 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1518 s_lastMouseDown
= 0;
1522 SystemClick( ev
, window
) ;
1523 s_lastMouseDown
= 0;
1526 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1532 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1537 Point pt
= { 0, 0 } ;
1538 SetPortWindowPort(window
) ;
1539 LocalToGlobal( &pt
) ;
1541 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1542 -1 , wxSIZE_USE_EXISTING
);
1544 s_lastMouseDown
= 0;
1548 if (TrackGoAway(window
, ev
->where
))
1553 s_lastMouseDown
= 0;
1557 Rect newContentRect
;
1558 Rect constraintRect
;
1559 constraintRect
.top
= win
->GetMinHeight() ;
1560 if ( constraintRect
.top
== -1 )
1561 constraintRect
.top
= 0 ;
1562 constraintRect
.left
= win
->GetMinWidth() ;
1563 if ( constraintRect
.left
== -1 )
1564 constraintRect
.left
= 0 ;
1565 constraintRect
.right
= win
->GetMaxWidth() ;
1566 if ( constraintRect
.right
== -1 )
1567 constraintRect
.right
= 32000 ;
1568 constraintRect
.bottom
= win
->GetMaxHeight() ;
1569 if ( constraintRect
.bottom
== -1 )
1570 constraintRect
.bottom
= 32000 ;
1572 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1573 &constraintRect
, &newContentRect
) ;
1576 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1577 newContentRect
.right
- newContentRect
.left
,
1578 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1580 s_lastMouseDown
= 0;
1585 if (TrackBox(window
, ev
->where
, windowPart
))
1587 // TODO setup size event
1588 ZoomWindow( window
, windowPart
, false ) ;
1594 Point pt
= { 0, 0 } ;
1595 SetPortWindowPort(window
) ;
1596 LocalToGlobal( &pt
) ;
1599 GetWindowPortBounds(window
, &tempRect
) ;
1600 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1601 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1604 s_lastMouseDown
= 0;
1606 case inCollapseBox
:
1607 // TODO setup size event
1608 s_lastMouseDown
= 0;
1615 SetPortWindowPort(window
) ;
1618 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1620 if ( s_macIsInModalLoop
)
1624 else if ( UMAIsWindowFloating( window
) )
1627 win
->MacMouseDown( ev
, windowPart
) ;
1631 // Activate window first
1632 ::SelectWindow( window
) ;
1636 win
->MacMouseDown( ev
, windowPart
) ;
1642 win
->MacMouseDown( ev
, windowPart
) ;
1650 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1652 EventRecord
* ev
= (EventRecord
*) evr
;
1655 short windowPart
= inNoWindow
;
1656 if ( wxTheApp
->s_captureWindow
)
1658 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1659 windowPart
= inContent
;
1663 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1674 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1676 win
->MacMouseUp( ev
, windowPart
) ;
1684 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1685 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1690 case kHomeCharCode
:
1693 case kEnterCharCode
:
1694 retval
= WXK_RETURN
;
1699 case kHelpCharCode
:
1702 case kBackspaceCharCode
:
1708 case kPageUpCharCode
:
1709 retval
= WXK_PAGEUP
;
1711 case kPageDownCharCode
:
1712 retval
= WXK_PAGEDOWN
;
1714 case kReturnCharCode
:
1715 retval
= WXK_RETURN
;
1717 case kFunctionKeyCharCode
:
1769 case kEscapeCharCode
:
1770 retval
= WXK_ESCAPE
;
1772 case kLeftArrowCharCode
:
1775 case kRightArrowCharCode
:
1776 retval
= WXK_RIGHT
;
1778 case kUpArrowCharCode
:
1781 case kDownArrowCharCode
:
1784 case kDeleteCharCode
:
1785 retval
= WXK_DELETE
;
1793 int wxKeyCodeToMacModifier(wxKeyCode key
)
1818 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1821 // wxHIDKeyboard keyboard;
1822 // return keyboard.IsActive(key);
1824 // TODO: Have it use HID Manager on OSX...
1825 //if OS X > 10.2 (i.e. 10.2.x)
1826 //a known apple bug prevents the system from determining led
1827 //states with GetKeys... can only determine caps lock led
1828 return !!(GetCurrentKeyModifiers() & wxKeyCodeToMacModifier(key
));
1830 // KeyMapByteArray keymap;
1831 // GetKeys((BigEndianLong*)keymap);
1832 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1837 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1839 EventRecord
* ev
= (EventRecord
*) evr
;
1840 wxToolTip::RemoveToolTips() ;
1842 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1843 if ( HiWord( menuresult
) )
1845 if ( !s_macIsInModalLoop
)
1846 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1850 wxWindow
* focus
= wxWindow::FindFocus() ;
1852 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1855 // we must handle control keys the other way round, otherwise text content is updated too late
1856 // has not been handled -> perform default
1857 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1858 if ( control
&& control
->GetMacControl() != NULL
)
1862 keychar
= short(ev
->message
& charCodeMask
);
1863 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1864 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1871 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1873 EventRecord
* ev
= (EventRecord
*) evr
;
1874 wxToolTip::RemoveToolTips() ;
1876 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1877 if ( HiWord( menuresult
) )
1882 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1888 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1895 keychar
= short(keymessage
& charCodeMask
);
1896 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1898 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1900 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1901 // and look at the character after
1903 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1904 keychar
= short(keyInfo
& charCodeMask
);
1905 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1907 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1908 long realkeyval
= keyval
;
1909 if ( keyval
== keychar
)
1911 // 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)
1912 realkeyval
= short(keymessage
& charCodeMask
) ;
1913 keyval
= wxToupper( keyval
) ;
1916 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1917 bool handled
= false ;
1918 event
.m_shiftDown
= modifiers
& shiftKey
;
1919 event
.m_controlDown
= modifiers
& controlKey
;
1920 event
.m_altDown
= modifiers
& optionKey
;
1921 event
.m_metaDown
= modifiers
& cmdKey
;
1922 event
.m_keyCode
= keyval
;
1926 event
.m_timeStamp
= when
;
1927 event
.SetEventObject(focus
);
1928 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1929 if ( handled
&& event
.GetSkipped() )
1936 wxWindow
*ancestor
= focus
;
1939 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1942 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1943 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1946 if (ancestor
->IsTopLevel())
1948 ancestor
= ancestor
->GetParent();
1951 #endif // wxUSE_ACCEL
1955 event
.Skip( FALSE
) ;
1956 event
.SetEventType( wxEVT_CHAR
) ;
1958 event
.m_keyCode
= realkeyval
;
1960 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1961 if ( handled
&& event
.GetSkipped() )
1965 (keyval
== WXK_TAB
) &&
1966 // CS: copied the change below from wxGTK
1967 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1968 // have this style, yet choose not to process this particular TAB in which
1969 // case TAB must still work as a navigational character
1971 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
1973 (focus
->GetParent()) &&
1974 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
1976 wxNavigationKeyEvent new_event
;
1977 new_event
.SetEventObject( focus
);
1978 new_event
.SetDirection( !event
.ShiftDown() );
1979 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1980 new_event
.SetWindowChange( event
.ControlDown() );
1981 new_event
.SetCurrentFocus( focus
);
1982 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1983 if ( handled
&& new_event
.GetSkipped() )
1986 // backdoor handler for default return and command escape
1987 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1989 // if window is not having a focus still testing for default enter or cancel
1990 // TODO add the UMA version for ActiveNonFloatingWindow
1991 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1994 if ( keyval
== WXK_RETURN
)
1996 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1998 if ( def
&& def
->IsEnabled() )
2000 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
2001 event
.SetEventObject(def
);
2002 def
->Command(event
);
2006 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
2007 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
2009 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
2010 new_event
.SetEventObject( focus
);
2011 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2018 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
2025 keychar
= short(keymessage
& charCodeMask
);
2026 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
2027 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
2029 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2030 // and look at the character after
2032 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2033 keychar
= short(keyInfo
& charCodeMask
);
2034 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2036 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2038 if ( keyval
== keychar
)
2040 keyval
= wxToupper( keyval
) ;
2042 bool handled
= false ;
2044 wxKeyEvent
event(wxEVT_KEY_UP
);
2045 event
.m_shiftDown
= modifiers
& shiftKey
;
2046 event
.m_controlDown
= modifiers
& controlKey
;
2047 event
.m_altDown
= modifiers
& optionKey
;
2048 event
.m_metaDown
= modifiers
& cmdKey
;
2049 event
.m_keyCode
= keyval
;
2053 event
.m_timeStamp
= when
;
2054 event
.SetEventObject(focus
);
2055 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2061 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
2063 EventRecord
* ev
= (EventRecord
*) evr
;
2064 WindowRef window
= (WindowRef
) ev
->message
;
2067 bool activate
= (ev
->modifiers
& activeFlag
) ;
2068 WindowClass wclass
;
2069 ::GetWindowClass ( window
, &wclass
) ;
2070 if ( wclass
== kFloatingWindowClass
)
2072 // if it is a floater we activate/deactivate the front non-floating window instead
2073 window
= ::FrontNonFloatingWindow() ;
2075 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2077 win
->MacActivate( ev
->when
, activate
) ;
2081 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
2083 EventRecord
* ev
= (EventRecord
*) evr
;
2084 WindowRef window
= (WindowRef
) ev
->message
;
2085 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2088 if ( !wxPendingDelete
.Member(win
) )
2089 win
->MacUpdate( ev
->when
) ;
2093 // since there is no way of telling this foreign window to update itself
2094 // we have to invalidate the update region otherwise we keep getting the same
2095 // event over and over again
2096 BeginUpdate( window
) ;
2097 EndUpdate( window
) ;
2101 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
2103 EventRecord
* ev
= (EventRecord
*) evr
;
2104 if ( HiWord( ev
->message
) != noErr
)
2108 SetPt( &point
, 100 , 100 ) ;
2110 err
= DIBadMount( point
, ev
->message
) ;
2111 wxASSERT( err
== noErr
) ;
2115 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2117 EventRecord
* ev
= (EventRecord
*) evr
;
2118 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2120 case suspendResumeMessage
:
2122 bool isResuming
= ev
->message
& resumeFlag
;
2123 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2125 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2128 WindowRef oldFrontWindow
= NULL
;
2129 WindowRef newFrontWindow
= NULL
;
2131 // in case we don't take care of activating ourselves, we have to synchronize
2132 // our idea of the active window with the process manager's - which it already activated
2134 if ( !doesActivate
)
2135 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2137 MacResume( convertClipboard
) ;
2139 newFrontWindow
= ::FrontNonFloatingWindow() ;
2141 if ( oldFrontWindow
)
2143 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2145 win
->MacActivate( ev
->when
, false ) ;
2147 if ( newFrontWindow
)
2149 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2151 win
->MacActivate( ev
->when
, true ) ;
2156 MacSuspend( convertClipboard
) ;
2160 case mouseMovedMessage
:
2164 wxWindow
* currentMouseWindow
= NULL
;
2166 if (s_captureWindow
)
2168 currentMouseWindow
= s_captureWindow
;
2172 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2173 ¤tMouseWindow
) ;
2176 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2178 wxMouseEvent event
;
2180 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2181 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2183 event
.m_leftDown
= isDown
&& !controlDown
;
2184 event
.m_middleDown
= FALSE
;
2185 event
.m_rightDown
= isDown
&& controlDown
;
2186 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2187 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2188 event
.m_altDown
= ev
->modifiers
& optionKey
;
2189 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2190 event
.m_x
= ev
->where
.h
;
2191 event
.m_y
= ev
->where
.v
;
2192 event
.m_timeStamp
= ev
->when
;
2193 event
.SetEventObject(this);
2195 if ( wxWindow::s_lastMouseWindow
)
2197 wxMouseEvent
eventleave(event
);
2198 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2199 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2200 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2202 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2204 if ( currentMouseWindow
)
2206 wxMouseEvent
evententer(event
);
2207 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2208 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2209 evententer
.SetEventObject( currentMouseWindow
) ;
2210 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2212 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2215 short windowPart
= inNoWindow
;
2217 if ( s_captureWindow
)
2219 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2220 windowPart
= inContent
;
2224 windowPart
= ::FindWindow(ev
->where
, &window
);
2231 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2233 win
->MacMouseMoved( ev
, windowPart
) ;
2240 UMAShowArrowCursor();
2250 UMAShowArrowCursor();
2261 void wxApp::MacHandleMouseMovedEvent(wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
2265 wxWindow
* currentMouseWindow
= NULL
;
2267 if (s_captureWindow
)
2269 currentMouseWindow
= s_captureWindow
;
2273 wxWindow::MacGetWindowFromPoint( wxPoint( x
, y
) , ¤tMouseWindow
) ;
2276 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2278 wxMouseEvent event
;
2280 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
2281 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
2283 event
.m_leftDown
= isDown
&& !controlDown
;
2285 event
.m_middleDown
= FALSE
;
2286 event
.m_rightDown
= isDown
&& controlDown
;
2288 event
.m_shiftDown
= modifiers
& shiftKey
;
2289 event
.m_controlDown
= modifiers
& controlKey
;
2290 event
.m_altDown
= modifiers
& optionKey
;
2291 event
.m_metaDown
= modifiers
& cmdKey
;
2295 event
.m_timeStamp
= timestamp
;
2297 if ( wxWindow::s_lastMouseWindow
)
2299 wxMouseEvent
eventleave(event
);
2300 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2301 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2302 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2305 wxToolTip::RelayEvent( wxWindow::s_lastMouseWindow
, eventleave
);
2306 #endif // wxUSE_TOOLTIPS
2307 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2309 if ( currentMouseWindow
)
2311 wxMouseEvent
evententer(event
);
2312 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2313 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2314 evententer
.SetEventObject( currentMouseWindow
) ;
2316 wxToolTip::RelayEvent( currentMouseWindow
, evententer
);
2317 #endif // wxUSE_TOOLTIPS
2318 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2320 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2323 short windowPart
= inNoWindow
;
2325 if ( s_captureWindow
)
2327 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2328 windowPart
= inContent
;
2332 Point pt
= { y
, x
} ;
2333 windowPart
= ::FindWindow(pt
, &window
);
2340 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2342 win
->MacFireMouseEvent( nullEvent
, x
, y
, modifiers
, timestamp
) ;
2349 UMAShowArrowCursor();
2359 UMAShowArrowCursor();
2366 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2368 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2369 wxFrame
* frame
= mbar
->GetFrame();
2370 wxCHECK_RET( mbar
!= NULL
&& frame
!= NULL
, wxT("error in menu item callback") );
2373 frame
->ProcessCommand(id
);
2378 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2381 return; // no menu item selected
2383 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2386 Str255 deskAccessoryName
;
2389 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2390 GetPort(&savedPort
);
2391 OpenDeskAcc(deskAccessoryName
);
2398 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2399 MacHandleMenuCommand( id
) ;