1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "app.h"
18 #include "wx/window.h"
20 #include "wx/button.h"
23 #include "wx/gdicmn.h"
26 #include "wx/cursor.h"
29 #include "wx/palette.h"
31 #include "wx/dialog.h"
32 #include "wx/msgdlg.h"
34 #include "wx/module.h"
35 #include "wx/memory.h"
36 #include "wx/tooltip.h"
37 #include "wx/textctrl.h"
39 #include "wx/docview.h"
53 #include "wx/mac/uma.h"
54 #include "wx/mac/macnotfy.h"
57 # include <CoreServices/CoreServices.h>
58 # if defined(WXMAKINGDLL_CORE)
59 # include <mach-o/dyld.h>
64 # include <ToolUtils.h>
65 # include <DiskInit.h>
69 extern wxList wxPendingDelete
;
70 extern wxList
*wxWinMacWindowList
;
71 extern wxList
*wxWinMacControlList
;
73 // statics for implementation
75 static bool s_inYield
= FALSE
;
78 static bool s_inReceiveEvent
= FALSE
;
79 static EventTime sleepTime
= kEventDurationNoWait
;
81 static long sleepTime
= 0 ;
84 #if !USE_SHARED_LIBRARY
85 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
86 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
87 EVT_IDLE(wxApp
::OnIdle
)
88 EVT_END_SESSION(wxApp
::OnEndSession
)
89 EVT_QUERY_END_SESSION(wxApp
::OnQueryEndSession
)
94 const short kMacMinHeap
= (29 * 1024) ;
95 // platform specific static variables
97 const short kwxMacMenuBarResource
= 1 ;
98 const short kwxMacAppleMenuId
= 1 ;
100 WXHRGN wxApp
::s_macCursorRgn
= NULL
;
101 wxWindow
* wxApp
::s_captureWindow
= NULL
;
102 int wxApp
::s_lastMouseDown
= 0 ;
103 long wxApp
::sm_lastMessageTime
= 0;
104 long wxApp
::s_lastModifiers
= 0 ;
107 bool wxApp
::s_macSupportPCMenuShortcuts
= true ;
108 long wxApp
::s_macAboutMenuItemId
= wxID_ABOUT
;
109 long wxApp
::s_macPreferencesMenuItemId
= wxID_PREFERENCES
;
110 long wxApp
::s_macExitMenuItemId
= wxID_EXIT
;
111 wxString wxApp
::s_macHelpMenuTitleName
= wxT("&Help") ;
113 // Normally we're not a plugin
114 bool wxApp
::sm_isEmbedded
= false;
115 //----------------------------------------------------------------------
116 // Core Apple Event Support
117 //----------------------------------------------------------------------
119 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
120 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
121 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
122 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
124 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
126 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
129 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
131 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
134 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
136 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
139 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
141 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
144 // AEODoc Calls MacOpenFile on each of the files passed
146 short wxApp
::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
150 DescType returnedType
;
156 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
160 err
= AECountItems(&docList
, &itemsInList
);
164 ProcessSerialNumber PSN
;
165 PSN
.highLongOfPSN
= 0 ;
166 PSN
.lowLongOfPSN
= kCurrentProcess
;
167 SetFrontProcess( &PSN
) ;
169 for (i
= 1; i
<= itemsInList
; i
++) {
170 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
171 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
172 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
178 // AEPDoc Calls MacPrintFile on each of the files passed
180 short wxApp
::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
184 DescType returnedType
;
190 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
194 err
= AECountItems(&docList
, &itemsInList
);
198 ProcessSerialNumber PSN
;
199 PSN
.highLongOfPSN
= 0 ;
200 PSN
.lowLongOfPSN
= kCurrentProcess
;
201 SetFrontProcess( &PSN
) ;
203 for (i
= 1; i
<= itemsInList
; i
++) {
204 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
205 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
206 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
212 // AEOApp calls MacNewFile
214 short wxApp
::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
220 // AEQuit attempts to quit the application
222 short wxApp
::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
224 wxWindow
* win
= GetTopWindow() ;
236 //----------------------------------------------------------------------
237 // Support Routines linking the Mac...File Calls to the Document Manager
238 //----------------------------------------------------------------------
240 void wxApp
::MacOpenFile(const wxString
& fileName
)
242 wxDocManager
* dm
= wxDocManager
::GetDocumentManager() ;
244 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
247 void wxApp
::MacPrintFile(const wxString
& fileName
)
249 wxDocManager
* dm
= wxDocManager
::GetDocumentManager() ;
252 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
255 wxView
* view
= doc
->GetFirstView() ;
258 wxPrintout
*printout
= view
->OnCreatePrintout();
262 printer
.Print(view
->GetFrame(), printout
, TRUE
);
268 doc
->DeleteAllViews();
269 dm
->RemoveDocument(doc
) ;
275 void wxApp
::MacNewFile()
279 //----------------------------------------------------------------------
280 // Carbon Event Handler
281 //----------------------------------------------------------------------
285 static const EventTypeSpec eventList
[] =
287 { kEventClassCommand
, kEventProcessCommand
} ,
288 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
290 { kEventClassMenu
, kEventMenuOpening
},
291 { kEventClassMenu
, kEventMenuClosed
},
293 { kEventClassApplication
, kEventAppActivated
} ,
294 { kEventClassApplication
, kEventAppDeactivated
} ,
295 // handling the quit event is not recommended by apple
296 // rather using the quit apple event - which we do
298 { kEventClassAppleEvent
, kEventAppleEvent
} ,
300 { kEventClassMouse
, kEventMouseDown
} ,
301 { kEventClassMouse
, kEventMouseMoved
} ,
302 { kEventClassMouse
, kEventMouseUp
} ,
303 { kEventClassMouse
, kEventMouseDragged
} ,
307 static pascal OSStatus
308 MenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
310 // FIXME: this doesn't work for multiple windows
311 wxWindow
*win
= wxTheApp
->GetTopWindow();
314 // VZ: we could find the menu from its handle here by examining all
315 // the menus in the menu bar recursively but knowing that neither
316 // wxMSW nor wxGTK do it why bother...
320 GetEventParameter(event
,
321 kEventParamDirectObject
,
323 sizeof(menuRef
), NULL
,
327 wxMenuEvent
wxevent(GetEventKind(event
) == kEventMenuOpening
330 wxevent
.SetEventObject(win
);
332 (void)win
->GetEventHandler()->ProcessEvent(wxevent
);
335 return eventNotHandledErr
;
338 // due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop
339 // but have to use ReceiveNextEvent dealing with events manually, therefore we also have
340 // deal with clicks in the menu bar explicitely
342 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
) ;
344 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
346 OSStatus result
= eventNotHandledErr
;
349 UInt32 modifiers
= 0;
350 EventMouseButton button
= 0 ;
353 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
354 sizeof( Point
), NULL
, &point
);
355 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
,
356 sizeof( UInt32
), NULL
, &modifiers
);
357 GetEventParameter( event
, kEventParamMouseButton
, typeMouseButton
, NULL
,
358 sizeof( EventMouseButton
), NULL
, &button
);
359 GetEventParameter( event
, kEventParamClickCount
, typeUInt32
, NULL
,
360 sizeof( UInt32
), NULL
, &click
);
362 if ( button
== 0 || GetEventKind( event
) == kEventMouseUp
)
363 modifiers
+= btnState
;
366 switch( GetEventKind(event
) )
368 case kEventMouseDown
:
372 short windowPart
= ::FindWindow(point
, &window
);
374 if ( windowPart
== inMenuBar
)
376 MenuSelect( point
) ;
381 case kEventMouseDragged
:
384 if ( wxTheApp
->s_captureWindow
)
385 wxMacWindowEventHandler( handler
, event
, (void*) wxTheApp
->s_captureWindow
->MacGetTopLevelWindow() ) ;
388 case kEventMouseMoved
:
390 wxTheApp
->MacHandleMouseMovedEvent( point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
400 static pascal OSStatus
CommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
402 OSStatus result
= eventNotHandledErr
;
406 GetEventParameter( event
, kEventParamDirectObject
, typeHICommand
, NULL
,
407 sizeof( HICommand
), NULL
, &command
);
409 MenuCommand id
= command
.commandID
;
410 if ( id
== kHICommandPreferences
)
411 id
= wxApp
::s_macPreferencesMenuItemId
;
413 wxMenuBar
* mbar
= wxMenuBar
::MacGetInstalledMenuBar() ;
414 wxMenu
* menu
= NULL
;
415 wxMenuItem
* item
= NULL
;
419 item
= mbar
->FindItem( id
, &menu
) ;
420 // it is not 100 % sure that an menu of id 0 is really ours, safety check
421 if ( id
== 0 && menu
!= NULL
&& menu
->GetHMenu() != command
.menu
.menuRef
)
428 if ( item
== NULL
|| menu
== NULL
|| mbar
== NULL
)
431 switch( GetEventKind( event
) )
433 case kEventProcessCommand
:
435 if (item
->IsCheckable())
437 item
->Check( !item
->IsChecked() ) ;
440 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
444 case kEventCommandUpdateStatus
:
445 // eventually trigger an updateui round
455 static pascal OSStatus
ApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
457 OSStatus result
= eventNotHandledErr
;
458 switch ( GetEventKind( event
) )
460 case kEventAppActivated
:
463 wxTheApp
->MacResume( true ) ;
467 case kEventAppDeactivated
:
470 wxTheApp
->MacSuspend( true ) ;
480 pascal OSStatus
wxAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
482 OSStatus result
= eventNotHandledErr
;
483 switch( GetEventClass( event
) )
485 case kEventClassCommand
:
486 result
= CommandEventHandler( handler
, event
, data
) ;
488 case kEventClassApplication
:
489 result
= ApplicationEventHandler( handler
, event
, data
) ;
491 case kEventClassMenu
:
492 result
= MenuEventHandler( handler
, event
, data
) ;
494 case kEventClassMouse
:
495 result
= MouseEventHandler( handler
, event
, data
) ;
497 case kEventClassAppleEvent
:
500 wxMacConvertEventToRecord( event
, &rec
) ;
501 result
= AEProcessAppleEvent( &rec
) ;
511 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler
)
515 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
516 // we know it's there ;-)
517 WXIMPORT
char std
::__throws_bad_alloc
;
520 bool wxApp
::Initialize(int& argc
, wxChar
**argv
)
526 UMAInitToolbox( 4, sm_isEmbedded
) ;
527 SetEventMask( everyEvent
) ;
528 UMAShowWatchCursor() ;
530 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
531 // open shared library resources from here since we don't have
532 // __wxinitialize in Mach-O shared libraries
533 wxStAppResource
::OpenSharedLibraryResource(NULL
);
537 // test the minimal configuration necessary
543 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
545 error
= kMacSTRWrongMachine
;
547 else if (theMachine
< gestaltMacPlus
)
549 error
= kMacSTRWrongMachine
;
551 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
553 error
= kMacSTROldSystem
;
555 else if ( theSystem
< 0x0860 )
557 error
= kMacSTROldSystem
;
559 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
561 error
= kMacSTRSmallSize
;
567 if ( !UMAHasAppearance() )
569 error = kMacSTRNoPre8Yet ;
575 // if we encountered any problems so far, give the error code and exit immediately
579 wxStAppResource resload
;
583 GetIndString(message
, 128, error
);
584 UMAShowArrowCursor() ;
585 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
586 itemHit
= Alert(128, nil
);
591 # if __option(profile)
592 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
597 // now avoid exceptions thrown for new (bad_alloc)
598 // FIXME CS for some changes outside wxMac does not compile anymore
600 std
::__throws_bad_alloc
= 0 ;
605 s_macCursorRgn
= ::NewRgn() ;
607 // Mac OS X passes a process serial number command line argument when
608 // the application is launched from the Finder. This argument must be
609 // removed from the command line arguments before being handled by the
610 // application (otherwise applications would need to handle it)
613 static const wxChar
*ARG_PSN
= _T("-psn_");
614 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
616 // remove this argument
618 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
622 if ( !wxAppBase
::Initialize(argc
, argv
) )
625 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
626 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
628 wxMacCreateNotifierTable() ;
630 UMAShowArrowCursor() ;
635 bool wxApp
::OnInitGui()
637 if( !wxAppBase
::OnInitGui() )
641 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
645 InstallApplicationEventHandler(
646 GetwxAppEventHandlerUPP(),
647 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
653 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
654 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
655 NewAEEventHandlerUPP(AEHandleODoc
) ,
657 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
658 NewAEEventHandlerUPP(AEHandleOApp
) ,
660 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
661 NewAEEventHandlerUPP(AEHandlePDoc
) ,
663 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
664 NewAEEventHandlerUPP(AEHandleQuit
) ,
667 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
668 NewAEEventHandlerProc(AEHandleODoc
) ,
670 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
671 NewAEEventHandlerProc(AEHandleOApp
) ,
673 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
674 NewAEEventHandlerProc(AEHandlePDoc
) ,
676 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
677 NewAEEventHandlerProc(AEHandleQuit
) ,
685 void wxApp
::CleanUp()
687 wxToolTip
::RemoveToolTips() ;
689 // One last chance for pending objects to be cleaned up
690 wxTheApp
->DeletePendingObjects();
692 wxMacDestroyNotifierTable() ;
694 delete wxWinMacWindowList
;
695 wxWinMacWindowList
= NULL
;
697 delete wxWinMacControlList
;
698 wxWinMacControlList
= NULL
;
701 # if __option(profile)
702 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
707 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
708 // close shared library resources from here since we don't have
709 // __wxterminate in Mach-O shared libraries
710 wxStAppResource
::CloseSharedLibraryResource();
713 UMACleanupToolbox() ;
714 if (s_macCursorRgn
) {
715 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
722 wxAppBase
::CleanUp();
725 //----------------------------------------------------------------------
726 // misc initialization stuff
727 //----------------------------------------------------------------------
729 // extern variable for shared library resource id
730 // need to be able to find it with NSLookupAndBindSymbol
731 short gSharedLibraryResource
= kResFileNotOpened
;
733 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
734 CFBundleRef gSharedLibraryBundle
= NULL
;
735 #endif /* WXMAKINGDLL_CORE && __DARWIN__ */
737 wxStAppResource
::wxStAppResource()
739 m_currentRefNum
= CurResFile() ;
740 if ( gSharedLibraryResource
!= kResFileNotOpened
)
742 UseResFile( gSharedLibraryResource
) ;
746 wxStAppResource
::~wxStAppResource()
748 if ( m_currentRefNum
!= kResFileNotOpened
)
750 UseResFile( m_currentRefNum
) ;
754 void wxStAppResource
::OpenSharedLibraryResource(const void *initBlock
)
756 gSharedLibraryResource
= kResFileNotOpened
;
758 #ifdef WXMAKINGDLL_CORE
759 if ( initBlock
!= NULL
) {
760 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
761 FSSpec
*fileSpec
= NULL
;
763 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
764 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
766 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
767 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
770 if (fileSpec
!= NULL
) {
771 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
776 // Open the shared library resource file if it is not yet open
779 const char *theLibPath
;
781 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
782 if (gSharedLibraryBundle
!= NULL
) {
783 // wxWindows has been bundled into a framework
784 // load the framework resources
786 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
789 // wxWindows is a simple dynamic shared library
790 // load the resources from the data fork of a separate resource file
794 OSErr theErr
= noErr
;
796 // get the library path
797 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
798 theModule
= NSModuleForSymbol(theSymbol
);
799 theLibPath
= NSLibraryNameForModule(theModule
);
801 // if we call wxLogDebug from here then, as wxTheApp hasn't been
802 // created yet when we're called from wxApp::Initialize(), wxLog
803 // is going to create a default stderr-based log target instead of
804 // the expected normal GUI one -- don't do it, if we really want
805 // to see this message just use fprintf() here
807 wxLogDebug( wxT("wxMac library installation name is '%s'"),
811 // allocate copy to replace .dylib.* extension with .rsrc
812 if (theLibPath
!= NULL
) {
813 theResPath
= theLibPath
;
814 // replace '_core' with '' in case of multi-lib build
815 theResPath
.Replace(wxT("_core"), wxEmptyString
);
816 // replace ".dylib" shared library extension with ".rsrc"
817 theResPath
.Replace(wxT(".dylib"), wxT(".rsrc"));
818 // Find the begining of the filename
819 theName
= theResPath
.AfterLast('/');
822 wxLogDebug( wxT("wxMac resources file name is '%s'"),
823 theResPath
.mb_str() );
826 theErr
= FSPathMakeRef((UInt8
*) theResPath
.mb_str(), &theResRef
, false);
827 if (theErr
!= noErr
) {
828 // try in current directory (using name only)
829 theErr
= FSPathMakeRef((UInt8
*) theName
.mb_str(), &theResRef
, false);
832 // open the resource file
833 if (theErr
== noErr
) {
834 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
835 &gSharedLibraryResource
);
837 if (theErr
!= noErr
) {
840 wxT("unable to open wxMac resource file '%s'\n"),
841 theResPath
.mb_str() );
842 #endif // __WXDEBUG__
847 #endif /* __DARWIN__ */
849 #endif /* WXMAKINGDLL_CORE */
852 void wxStAppResource
::CloseSharedLibraryResource()
854 #ifdef WXMAKINGDLL_CORE
855 // Close the shared library resource file
856 if (gSharedLibraryResource
!= kResFileNotOpened
) {
858 if (gSharedLibraryBundle
!= NULL
) {
859 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
860 gSharedLibraryResource
);
861 gSharedLibraryBundle
= NULL
;
864 #endif /* __DARWIN__ */
866 CloseResFile(gSharedLibraryResource
);
868 gSharedLibraryResource
= kResFileNotOpened
;
870 #endif /* WXMAKINGDLL_CORE */
873 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
875 // for shared libraries we have to manually get the correct resource
876 // ref num upon initializing and releasing when terminating, therefore
877 // the __wxinitialize and __wxterminate must be used
880 void __sinit(void); /* (generated by linker) */
881 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
882 pascal void __terminate(void);
885 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
887 wxStAppResource
::OpenSharedLibraryResource( theInitBlock
) ;
888 return __initialize( theInitBlock
) ;
891 pascal void __wxterminate(void)
893 wxStAppResource
::CloseSharedLibraryResource() ;
897 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
901 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
903 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
904 OSStatus err
= noErr
;
907 switch( GetEventClass( event
) )
909 case kEventClassKeyboard
:
912 switch( GetEventKind(event
) )
914 case kEventRawKeyDown
:
915 rec
->what
= keyDown
;
917 case kEventRawKeyRepeat
:
918 rec
->what
= autoKey
;
920 case kEventRawKeyUp
:
923 case kEventRawKeyModifiersChanged
:
924 rec
->what
= nullEvent
;
933 unsigned char charCode
;
935 GetMouse( &rec
->where
) ;
937 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
938 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
939 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
940 rec
->modifiers
= modifiers
;
941 rec
->message
= (keyCode
<< 8 ) + charCode
;
945 case kEventClassTextInput
:
947 switch( GetEventKind( event
) )
949 case kEventTextInputUnicodeForKeyEvent
:
952 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
956 unsigned char charCode
;
958 GetMouse( &rec
->where
) ;
959 rec
->what
= keyDown
;
960 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
961 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
962 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
963 rec
->modifiers
= modifiers
;
964 rec
->message
= (keyCode
<< 8 ) + charCode
;
980 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
982 OSStatus result = eventNotHandledErr ;
985 switch ( GetEventClass( event ) )
987 case kEventClassKeyboard :
988 if ( wxMacConvertEventToRecord( event , &rec ) )
990 wxTheApp->MacHandleModifierEvents( &rec ) ;
991 wxTheApp->MacHandleOneEvent( &rec ) ;
995 case kEventClassTextInput :
996 if ( wxMacConvertEventToRecord( event , &rec ) )
998 wxTheApp->MacHandleModifierEvents( &rec ) ;
999 wxTheApp->MacHandleOneEvent( &rec ) ;
1013 m_printMode
= wxPRINT_WINDOWS
;
1016 m_macCurrentEvent
= NULL
;
1018 m_macCurrentEventHandlerCallRef
= NULL
;
1022 int wxApp
::MainLoop()
1034 void wxApp
::ExitMainLoop()
1036 m_keepGoing
= FALSE
;
1039 // Is a message/event pending?
1040 bool wxApp
::Pending()
1043 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1047 return EventAvail( everyEvent
, &event
) ;
1051 // Dispatch a message.
1052 bool wxApp
::Dispatch()
1059 void wxApp
::OnIdle(wxIdleEvent
& event
)
1061 wxAppBase
::OnIdle(event
);
1063 // If they are pending events, we must process them: pending events are
1064 // either events to the threads other than main or events posted with
1065 // wxPostEvent() functions
1066 wxMacProcessNotifierAndPendingEvents();
1068 if(!wxMenuBar
::MacGetInstalledMenuBar() && wxMenuBar
::MacGetCommonMenuBar())
1069 wxMenuBar
::MacGetCommonMenuBar()->MacInstallMenuBar();
1072 void wxApp
::WakeUpIdle()
1083 void wxApp
::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1086 GetTopWindow()->Close(TRUE
);
1089 // Default behaviour: close the application with prompts. The
1090 // user can veto the close, and therefore the end session.
1091 void wxApp
::OnQueryEndSession(wxCloseEvent
& event
)
1095 if (!GetTopWindow()->Close(!event
.CanVeto()))
1100 extern "C" void wxCYield() ;
1106 // Yield to other processes
1108 bool wxApp
::Yield(bool onlyIfNeeded
)
1112 if ( !onlyIfNeeded
)
1114 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1123 YieldToAnyThread() ;
1125 // by definition yield should handle all non-processed events
1129 OSStatus status
= noErr
;
1132 s_inReceiveEvent
= true ;
1133 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1134 s_inReceiveEvent
= false ;
1136 if ( status
== eventLoopTimedOutErr
)
1138 // make sure next time the event loop will trigger idle events
1139 sleepTime
= kEventDurationNoWait
;
1141 else if ( status
== eventLoopQuitErr
)
1143 // according to QA1061 this may also occur when a WakeUp Process
1148 MacHandleOneEvent( theEvent
) ;
1149 ReleaseEvent(theEvent
);
1151 } while( status
== noErr
) ;
1155 // having a larger value here leads to large performance slowdowns
1156 // so we cannot give background apps more processor time here
1157 // we do so however having a large sleep value in the main event loop
1160 while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp
::s_macCursorRgn
))
1162 MacHandleModifierEvents( &event
) ;
1163 MacHandleOneEvent( &event
);
1164 if ( event
.what
!= kHighLevelEvent
)
1165 SetRectRgn( (RgnHandle
) wxApp
::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1167 MacHandleModifierEvents( &event
) ;
1170 wxMacProcessNotifierAndPendingEvents() ;
1176 // platform specifics
1178 void wxApp
::MacSuspend( bool convertClipboard
)
1181 // we have to deactive the top level windows manually
1183 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1186 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1188 #if 0 // having problems right now with that
1189 if (!win
->HasFlag(wxSTAY_ON_TOP
))
1192 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1194 node
= node
->GetNext();
1197 ::HideFloatingWindows() ;
1199 s_lastMouseDown
= 0 ;
1201 if( convertClipboard
)
1203 MacConvertPrivateToPublicScrap() ;
1207 extern wxList wxModalDialogs
;
1209 void wxApp
::MacResume( bool convertClipboard
)
1211 s_lastMouseDown
= 0 ;
1212 if( convertClipboard
)
1214 MacConvertPublicToPrivateScrap() ;
1218 ::ShowFloatingWindows() ;
1219 // raise modal dialogs in case a non modal window was selected to activate the app
1221 wxNode
* node
= wxModalDialogs
.GetFirst();
1224 wxDialog
* dialog
= (wxDialog
*) node
->GetData();
1227 node
= node
->GetNext();
1232 void wxApp
::MacConvertPrivateToPublicScrap()
1236 void wxApp
::MacConvertPublicToPrivateScrap()
1240 void wxApp
::MacDoOneEvent()
1245 s_inReceiveEvent
= true ;
1246 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1247 s_inReceiveEvent
= false ;
1248 if ( status
== eventLoopTimedOutErr
)
1250 if ( wxTheApp
->ProcessIdle() )
1251 sleepTime
= kEventDurationNoWait
;
1253 sleepTime
= kEventDurationForever
;
1255 else if ( status
== eventLoopQuitErr
)
1257 // according to QA1061 this may also occur when a WakeUp Process
1262 MacHandleOneEvent( theEvent
) ;
1263 ReleaseEvent(theEvent
);
1264 sleepTime
= kEventDurationNoWait
;
1269 EventMask eventMask
= everyEvent
;
1271 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1273 MacHandleModifierEvents( &event
) ;
1274 MacHandleOneEvent( &event
);
1278 MacHandleModifierEvents( &event
) ;
1280 WindowPtr window
= ::FrontWindow() ;
1282 ::IdleControls( window
) ;
1284 if ( wxTheApp
->ProcessIdle() )
1285 sleepTime
= kEventDurationNoWait
;
1287 sleepTime
= GetCaretTime() / 2 ;
1289 if ( event
.what
!= kHighLevelEvent
)
1290 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1294 DeletePendingObjects() ;
1295 wxMacProcessNotifierAndPendingEvents() ;
1298 /*virtual*/ void wxApp
::MacHandleUnhandledEvent( WXEVENTREF evr
)
1300 // Override to process unhandled events as you please
1303 void wxApp
::MacHandleOneEvent( WXEVENTREF evr
)
1306 EventTargetRef theTarget
;
1307 theTarget
= GetEventDispatcherTarget();
1308 m_macCurrentEvent
= evr
;
1309 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1310 if(status
== eventNotHandledErr
)
1312 MacHandleUnhandledEvent(evr
);
1315 EventRecord
* ev
= (EventRecord
*) evr
;
1316 m_macCurrentEvent
= ev
;
1318 wxApp
::sm_lastMessageTime
= ev
->when
;
1323 MacHandleMouseDownEvent( ev
) ;
1324 if ( ev
->modifiers
& controlKey
)
1325 s_lastMouseDown
= 2;
1327 s_lastMouseDown
= 1;
1330 if ( s_lastMouseDown
== 2 )
1332 ev
->modifiers
|= controlKey
;
1336 ev
->modifiers
&= ~controlKey
;
1338 MacHandleMouseUpEvent( ev
) ;
1339 s_lastMouseDown
= 0;
1342 MacHandleActivateEvent( ev
) ;
1345 // In embedded mode we first let the UnhandledEvent function
1346 // try to handle the update event. If we handle it ourselves
1347 // first and then pass it on, the host's windows won't update.
1348 MacHandleUnhandledEvent(ev
);
1349 MacHandleUpdateEvent( ev
) ;
1353 MacHandleKeyDownEvent( ev
) ;
1356 MacHandleKeyUpEvent( ev
) ;
1359 MacHandleDiskEvent( ev
) ;
1362 MacHandleOSEvent( ev
) ;
1364 case kHighLevelEvent
:
1365 MacHandleHighLevelEvent( ev
) ;
1371 wxMacProcessNotifierAndPendingEvents() ;
1375 bool s_macIsInModalLoop
= false ;
1377 void wxApp
::MacHandleModifierEvents( WXEVENTREF evr
)
1379 EventRecord
* ev
= (EventRecord
*) evr
;
1380 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow
::FindFocus() != NULL
)
1382 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1384 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1385 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1386 event
.m_altDown
= ev
->modifiers
& optionKey
;
1387 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1389 event
.m_x
= ev
->where
.h
;
1390 event
.m_y
= ev
->where
.v
;
1391 event
.m_timeStamp
= ev
->when
;
1392 wxWindow
* focus
= wxWindow
::FindFocus() ;
1393 event
.SetEventObject(focus
);
1395 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1397 event
.m_keyCode
= WXK_CONTROL
;
1398 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1399 focus
->GetEventHandler()->ProcessEvent( event
) ;
1401 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1403 event
.m_keyCode
= WXK_SHIFT
;
1404 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1405 focus
->GetEventHandler()->ProcessEvent( event
) ;
1407 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1409 event
.m_keyCode
= WXK_ALT
;
1410 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1411 focus
->GetEventHandler()->ProcessEvent( event
) ;
1413 if ( ( ev
->modifiers
^ s_lastModifiers
) & cmdKey
)
1415 event
.m_keyCode
= WXK_COMMAND
;
1416 event
.SetEventType( ( ev
->modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1417 focus
->GetEventHandler()->ProcessEvent( event
) ;
1419 s_lastModifiers
= ev
->modifiers
;
1423 void wxApp
::MacHandleHighLevelEvent( WXEVENTREF evr
)
1425 // we must avoid reentrancy problems when processing high level events eg printing
1426 bool former
= s_inYield
;
1428 EventRecord
* ev
= (EventRecord
*) evr
;
1429 ::AEProcessAppleEvent( ev
) ;
1430 s_inYield
= former
;
1433 void wxApp
::MacHandleMouseDownEvent( WXEVENTREF evr
)
1435 EventRecord
* ev
= (EventRecord
*) evr
;
1436 wxToolTip
::RemoveToolTips() ;
1439 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1440 WindowAttributes frontWindowAttributes
= NULL
;
1442 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1444 short windowPart
= ::FindWindow(ev
->where
, &window
);
1445 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1446 if ( wxPendingDelete
.Member(win
) )
1450 GetQDGlobalsScreenBits( &screenBits
);
1455 if ( s_macIsInModalLoop
)
1461 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1462 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1463 s_lastMouseDown
= 0;
1467 SystemClick( ev
, window
) ;
1468 s_lastMouseDown
= 0;
1471 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1477 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1482 Point pt
= { 0, 0 } ;
1483 SetPortWindowPort(window
) ;
1484 LocalToGlobal( &pt
) ;
1486 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1487 -1 , wxSIZE_USE_EXISTING
);
1489 s_lastMouseDown
= 0;
1493 if (TrackGoAway(window
, ev
->where
))
1498 s_lastMouseDown
= 0;
1502 Rect newContentRect
;
1503 Rect constraintRect
;
1504 constraintRect
.top
= win
->GetMinHeight() ;
1505 if ( constraintRect
.top
== -1 )
1506 constraintRect
.top
= 0 ;
1507 constraintRect
.left
= win
->GetMinWidth() ;
1508 if ( constraintRect
.left
== -1 )
1509 constraintRect
.left
= 0 ;
1510 constraintRect
.right
= win
->GetMaxWidth() ;
1511 if ( constraintRect
.right
== -1 )
1512 constraintRect
.right
= 32000 ;
1513 constraintRect
.bottom
= win
->GetMaxHeight() ;
1514 if ( constraintRect
.bottom
== -1 )
1515 constraintRect
.bottom
= 32000 ;
1517 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1518 &constraintRect
, &newContentRect
) ;
1521 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1522 newContentRect
.right
- newContentRect
.left
,
1523 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1525 s_lastMouseDown
= 0;
1530 if (TrackBox(window
, ev
->where
, windowPart
))
1532 // TODO setup size event
1533 ZoomWindow( window
, windowPart
, false ) ;
1539 Point pt
= { 0, 0 } ;
1540 SetPortWindowPort(window
) ;
1541 LocalToGlobal( &pt
) ;
1544 GetWindowPortBounds(window
, &tempRect
) ;
1545 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1546 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1549 s_lastMouseDown
= 0;
1551 case inCollapseBox
:
1552 // TODO setup size event
1553 s_lastMouseDown
= 0;
1560 SetPortWindowPort(window
) ;
1563 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1565 if ( s_macIsInModalLoop
)
1569 else if ( UMAIsWindowFloating( window
) )
1572 win
->MacMouseDown( ev
, windowPart
) ;
1576 // Activate window first
1577 ::SelectWindow( window
) ;
1581 win
->MacMouseDown( ev
, windowPart
) ;
1587 win
->MacMouseDown( ev
, windowPart
) ;
1595 void wxApp
::MacHandleMouseUpEvent( WXEVENTREF evr
)
1597 EventRecord
* ev
= (EventRecord
*) evr
;
1600 short windowPart
= inNoWindow
;
1601 if ( wxTheApp
->s_captureWindow
)
1603 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1604 windowPart
= inContent
;
1608 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1619 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1621 win
->MacMouseUp( ev
, windowPart
) ;
1629 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1630 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1635 case kHomeCharCode
:
1638 case kEnterCharCode
:
1639 retval
= WXK_RETURN
;
1644 case kHelpCharCode
:
1647 case kBackspaceCharCode
:
1653 case kPageUpCharCode
:
1654 retval
= WXK_PAGEUP
;
1656 case kPageDownCharCode
:
1657 retval
= WXK_PAGEDOWN
;
1659 case kReturnCharCode
:
1660 retval
= WXK_RETURN
;
1662 case kFunctionKeyCharCode
:
1714 case kEscapeCharCode
:
1715 retval
= WXK_ESCAPE
;
1717 case kLeftArrowCharCode
:
1720 case kRightArrowCharCode
:
1721 retval
= WXK_RIGHT
;
1723 case kUpArrowCharCode
:
1726 case kDownArrowCharCode
:
1729 case kDeleteCharCode
:
1730 retval
= WXK_DELETE
;
1739 void wxApp
::MacHandleKeyDownEvent( WXEVENTREF evr
)
1741 EventRecord
* ev
= (EventRecord
*) evr
;
1742 wxToolTip
::RemoveToolTips() ;
1744 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1745 if ( HiWord( menuresult
) )
1747 if ( !s_macIsInModalLoop
)
1748 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1752 wxWindow
* focus
= wxWindow
::FindFocus() ;
1754 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1757 // we must handle control keys the other way round, otherwise text content is updated too late
1758 // has not been handled -> perform default
1759 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1760 if ( control
&& control
->GetMacControl() != NULL
)
1764 keychar
= short(ev
->message
& charCodeMask
);
1765 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1766 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1773 void wxApp
::MacHandleKeyUpEvent( WXEVENTREF evr
)
1775 EventRecord
* ev
= (EventRecord
*) evr
;
1776 wxToolTip
::RemoveToolTips() ;
1778 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1779 if ( HiWord( menuresult
) )
1784 MacSendKeyUpEvent( wxWindow
::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1790 bool wxApp
::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1797 keychar
= short(keymessage
& charCodeMask
);
1798 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1800 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1802 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1803 // and look at the character after
1805 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1806 keychar
= short(keyInfo
& charCodeMask
);
1807 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1809 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1810 long realkeyval
= keyval
;
1811 if ( keyval
== keychar
)
1813 // 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)
1814 realkeyval
= short(keymessage
& charCodeMask
) ;
1815 keyval
= wxToupper( keyval
) ;
1818 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1819 bool handled
= false ;
1820 event
.m_shiftDown
= modifiers
& shiftKey
;
1821 event
.m_controlDown
= modifiers
& controlKey
;
1822 event
.m_altDown
= modifiers
& optionKey
;
1823 event
.m_metaDown
= modifiers
& cmdKey
;
1824 event
.m_keyCode
= keyval
;
1828 event
.m_timeStamp
= when
;
1829 event
.SetEventObject(focus
);
1830 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1831 if ( handled
&& event
.GetSkipped() )
1838 wxWindow
*ancestor
= focus
;
1841 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1844 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1845 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1848 if (ancestor
->IsTopLevel())
1850 ancestor
= ancestor
->GetParent();
1853 #endif // wxUSE_ACCEL
1857 event
.Skip( FALSE
) ;
1858 event
.SetEventType( wxEVT_CHAR
) ;
1860 event
.m_keyCode
= realkeyval
;
1862 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1863 if ( handled
&& event
.GetSkipped() )
1867 (keyval
== WXK_TAB
) &&
1868 // CS: copied the change below from wxGTK
1869 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1870 // have this style, yet choose not to process this particular TAB in which
1871 // case TAB must still work as a navigational character
1873 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
1875 (focus
->GetParent()) &&
1876 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
1878 wxNavigationKeyEvent new_event
;
1879 new_event
.SetEventObject( focus
);
1880 new_event
.SetDirection( !event
.ShiftDown() );
1881 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1882 new_event
.SetWindowChange( event
.ControlDown() );
1883 new_event
.SetCurrentFocus( focus
);
1884 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1885 if ( handled
&& new_event
.GetSkipped() )
1888 // backdoor handler for default return and command escape
1889 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1891 // if window is not having a focus still testing for default enter or cancel
1892 // TODO add the UMA version for ActiveNonFloatingWindow
1893 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1896 if ( keyval
== WXK_RETURN
)
1898 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1900 if ( def
&& def
->IsEnabled() )
1902 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1903 event
.SetEventObject(def
);
1904 def
->Command(event
);
1908 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1909 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1911 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1912 new_event
.SetEventObject( focus
);
1913 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1920 bool wxApp
::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1927 keychar
= short(keymessage
& charCodeMask
);
1928 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1929 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1931 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1932 // and look at the character after
1934 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1935 keychar
= short(keyInfo
& charCodeMask
);
1936 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1938 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1940 if ( keyval
== keychar
)
1942 keyval
= wxToupper( keyval
) ;
1944 bool handled
= false ;
1946 wxKeyEvent
event(wxEVT_KEY_UP
);
1947 event
.m_shiftDown
= modifiers
& shiftKey
;
1948 event
.m_controlDown
= modifiers
& controlKey
;
1949 event
.m_altDown
= modifiers
& optionKey
;
1950 event
.m_metaDown
= modifiers
& cmdKey
;
1951 event
.m_keyCode
= keyval
;
1955 event
.m_timeStamp
= when
;
1956 event
.SetEventObject(focus
);
1957 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1963 void wxApp
::MacHandleActivateEvent( WXEVENTREF evr
)
1965 EventRecord
* ev
= (EventRecord
*) evr
;
1966 WindowRef window
= (WindowRef
) ev
->message
;
1969 bool activate
= (ev
->modifiers
& activeFlag
) ;
1970 WindowClass wclass
;
1971 ::GetWindowClass ( window
, &wclass
) ;
1972 if ( wclass
== kFloatingWindowClass
)
1974 // if it is a floater we activate/deactivate the front non-floating window instead
1975 window
= ::FrontNonFloatingWindow() ;
1977 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1979 win
->MacActivate( ev
->when
, activate
) ;
1983 void wxApp
::MacHandleUpdateEvent( WXEVENTREF evr
)
1985 EventRecord
* ev
= (EventRecord
*) evr
;
1986 WindowRef window
= (WindowRef
) ev
->message
;
1987 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1990 if ( !wxPendingDelete
.Member(win
) )
1991 win
->MacUpdate( ev
->when
) ;
1995 // since there is no way of telling this foreign window to update itself
1996 // we have to invalidate the update region otherwise we keep getting the same
1997 // event over and over again
1998 BeginUpdate( window
) ;
1999 EndUpdate( window
) ;
2003 void wxApp
::MacHandleDiskEvent( WXEVENTREF evr
)
2005 EventRecord
* ev
= (EventRecord
*) evr
;
2006 if ( HiWord( ev
->message
) != noErr
)
2010 SetPt( &point
, 100 , 100 ) ;
2012 err
= DIBadMount( point
, ev
->message
) ;
2013 wxASSERT( err
== noErr
) ;
2017 void wxApp
::MacHandleOSEvent( WXEVENTREF evr
)
2019 EventRecord
* ev
= (EventRecord
*) evr
;
2020 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2022 case suspendResumeMessage
:
2024 bool isResuming
= ev
->message
& resumeFlag
;
2025 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2027 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2030 WindowRef oldFrontWindow
= NULL
;
2031 WindowRef newFrontWindow
= NULL
;
2033 // in case we don't take care of activating ourselves, we have to synchronize
2034 // our idea of the active window with the process manager's - which it already activated
2036 if ( !doesActivate
)
2037 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2039 MacResume( convertClipboard
) ;
2041 newFrontWindow
= ::FrontNonFloatingWindow() ;
2043 if ( oldFrontWindow
)
2045 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2047 win
->MacActivate( ev
->when
, false ) ;
2049 if ( newFrontWindow
)
2051 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2053 win
->MacActivate( ev
->when
, true ) ;
2058 MacSuspend( convertClipboard
) ;
2062 case mouseMovedMessage
:
2066 wxWindow
* currentMouseWindow
= NULL
;
2068 if (s_captureWindow
)
2070 currentMouseWindow
= s_captureWindow
;
2074 wxWindow
::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2075 ¤tMouseWindow
) ;
2078 if ( currentMouseWindow
!= wxWindow
::s_lastMouseWindow
)
2080 wxMouseEvent event
;
2082 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2083 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2085 event
.m_leftDown
= isDown
&& !controlDown
;
2086 event
.m_middleDown
= FALSE
;
2087 event
.m_rightDown
= isDown
&& controlDown
;
2088 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2089 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2090 event
.m_altDown
= ev
->modifiers
& optionKey
;
2091 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2092 event
.m_x
= ev
->where
.h
;
2093 event
.m_y
= ev
->where
.v
;
2094 event
.m_timeStamp
= ev
->when
;
2095 event
.SetEventObject(this);
2097 if ( wxWindow
::s_lastMouseWindow
)
2099 wxMouseEvent
eventleave(event
);
2100 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2101 wxWindow
::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2102 eventleave
.SetEventObject( wxWindow
::s_lastMouseWindow
) ;
2104 wxWindow
::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2106 if ( currentMouseWindow
)
2108 wxMouseEvent
evententer(event
);
2109 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2110 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2111 evententer
.SetEventObject( currentMouseWindow
) ;
2112 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2114 wxWindow
::s_lastMouseWindow
= currentMouseWindow
;
2117 short windowPart
= inNoWindow
;
2119 if ( s_captureWindow
)
2121 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2122 windowPart
= inContent
;
2126 windowPart
= ::FindWindow(ev
->where
, &window
);
2133 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2135 win
->MacMouseMoved( ev
, windowPart
) ;
2142 UMAShowArrowCursor();
2152 UMAShowArrowCursor();
2163 void wxApp
::MacHandleMouseMovedEvent(wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
2167 wxWindow
* currentMouseWindow
= NULL
;
2169 if (s_captureWindow
)
2171 currentMouseWindow
= s_captureWindow
;
2175 wxWindow
::MacGetWindowFromPoint( wxPoint( x
, y
) , ¤tMouseWindow
) ;
2178 if ( currentMouseWindow
!= wxWindow
::s_lastMouseWindow
)
2180 wxMouseEvent event
;
2182 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
2183 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
2185 event
.m_leftDown
= isDown
&& !controlDown
;
2187 event
.m_middleDown
= FALSE
;
2188 event
.m_rightDown
= isDown
&& controlDown
;
2190 event
.m_shiftDown
= modifiers
& shiftKey
;
2191 event
.m_controlDown
= modifiers
& controlKey
;
2192 event
.m_altDown
= modifiers
& optionKey
;
2193 event
.m_metaDown
= modifiers
& cmdKey
;
2197 event
.m_timeStamp
= timestamp
;
2199 if ( wxWindow
::s_lastMouseWindow
)
2201 wxMouseEvent
eventleave(event
);
2202 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2203 wxWindow
::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2204 eventleave
.SetEventObject( wxWindow
::s_lastMouseWindow
) ;
2207 wxToolTip
::RelayEvent( wxWindow
::s_lastMouseWindow
, eventleave
);
2208 #endif // wxUSE_TOOLTIPS
2209 wxWindow
::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2211 if ( currentMouseWindow
)
2213 wxMouseEvent
evententer(event
);
2214 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2215 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2216 evententer
.SetEventObject( currentMouseWindow
) ;
2218 wxToolTip
::RelayEvent( currentMouseWindow
, evententer
);
2219 #endif // wxUSE_TOOLTIPS
2220 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2222 wxWindow
::s_lastMouseWindow
= currentMouseWindow
;
2225 short windowPart
= inNoWindow
;
2227 if ( s_captureWindow
)
2229 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2230 windowPart
= inContent
;
2234 Point pt
= { y
, x
} ;
2235 windowPart
= ::FindWindow(pt
, &window
);
2242 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2244 win
->MacFireMouseEvent( nullEvent
, x
, y
, modifiers
, timestamp
) ;
2251 UMAShowArrowCursor();
2261 UMAShowArrowCursor();
2268 void wxApp
::MacHandleMenuCommand( wxUint32 id
)
2270 wxMenuBar
* mbar
= wxMenuBar
::MacGetInstalledMenuBar() ;
2271 wxFrame
* frame
= mbar
->GetFrame();
2272 wxCHECK_RET( mbar
!= NULL
&& frame
!= NULL
, wxT("error in menu item callback") );
2275 frame
->ProcessCommand(id
);
2280 void wxApp
::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2283 return; // no menu item selected
2285 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2288 Str255 deskAccessoryName
;
2291 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2292 GetPort(&savedPort
);
2293 OpenDeskAcc(deskAccessoryName
);
2300 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2301 MacHandleMenuCommand( id
) ;