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
) )
650 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
651 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
653 wxMacCreateNotifierTable() ;
655 UMAShowArrowCursor() ;
660 bool wxApp::OnInitGui()
662 if( !wxAppBase::OnInitGui() )
666 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
670 InstallApplicationEventHandler(
671 GetwxAppEventHandlerUPP(),
672 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
678 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
679 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
680 NewAEEventHandlerUPP(AEHandleODoc
) ,
682 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
683 NewAEEventHandlerUPP(AEHandleOApp
) ,
685 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
686 NewAEEventHandlerUPP(AEHandlePDoc
) ,
688 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
689 NewAEEventHandlerUPP(AEHandleQuit
) ,
692 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
693 NewAEEventHandlerProc(AEHandleODoc
) ,
695 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
696 NewAEEventHandlerProc(AEHandleOApp
) ,
698 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
699 NewAEEventHandlerProc(AEHandlePDoc
) ,
701 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
702 NewAEEventHandlerProc(AEHandleQuit
) ,
710 void wxApp::CleanUp()
712 wxToolTip::RemoveToolTips() ;
714 // One last chance for pending objects to be cleaned up
715 wxTheApp
->DeletePendingObjects();
717 wxMacDestroyNotifierTable() ;
719 delete wxWinMacWindowList
;
720 wxWinMacWindowList
= NULL
;
722 delete wxWinMacControlList
;
723 wxWinMacControlList
= NULL
;
726 # if __option(profile)
727 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
732 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
733 // close shared library resources from here since we don't have
734 // __wxterminate in Mach-O shared libraries
735 wxStAppResource::CloseSharedLibraryResource();
738 UMACleanupToolbox() ;
739 if (s_macCursorRgn
) {
740 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
747 wxAppBase::CleanUp();
750 //----------------------------------------------------------------------
751 // misc initialization stuff
752 //----------------------------------------------------------------------
754 // extern variable for shared library resource id
755 // need to be able to find it with NSLookupAndBindSymbol
756 short gSharedLibraryResource
= kResFileNotOpened
;
758 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
759 CFBundleRef gSharedLibraryBundle
= NULL
;
760 #endif /* WXMAKINGDLL_CORE && __DARWIN__ */
762 wxStAppResource::wxStAppResource()
764 m_currentRefNum
= CurResFile() ;
765 if ( gSharedLibraryResource
!= kResFileNotOpened
)
767 UseResFile( gSharedLibraryResource
) ;
771 wxStAppResource::~wxStAppResource()
773 if ( m_currentRefNum
!= kResFileNotOpened
)
775 UseResFile( m_currentRefNum
) ;
779 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
781 gSharedLibraryResource
= kResFileNotOpened
;
783 #ifdef WXMAKINGDLL_CORE
784 if ( initBlock
!= NULL
) {
785 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
786 FSSpec
*fileSpec
= NULL
;
788 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
789 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
791 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
792 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
795 if (fileSpec
!= NULL
) {
796 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
801 // Open the shared library resource file if it is not yet open
804 const char *theLibPath
;
806 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
807 if (gSharedLibraryBundle
!= NULL
) {
808 // wxWindows has been bundled into a framework
809 // load the framework resources
811 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
814 // wxWindows is a simple dynamic shared library
815 // load the resources from the data fork of a separate resource file
819 OSErr theErr
= noErr
;
821 // get the library path
822 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
823 theModule
= NSModuleForSymbol(theSymbol
);
824 theLibPath
= NSLibraryNameForModule(theModule
);
826 // if we call wxLogDebug from here then, as wxTheApp hasn't been
827 // created yet when we're called from wxApp::Initialize(), wxLog
828 // is going to create a default stderr-based log target instead of
829 // the expected normal GUI one -- don't do it, if we really want
830 // to see this message just use fprintf() here
832 wxLogDebug( wxT("wxMac library installation name is '%s'"),
836 // allocate copy to replace .dylib.* extension with .rsrc
837 if (theLibPath
!= NULL
) {
839 theResPath
= wxString(theLibPath
, wxConvLocal
);
841 theResPath
= wxString(theLibPath
);
843 // replace '_core' with '' in case of multi-lib build
844 theResPath
.Replace(wxT("_core"), wxEmptyString
);
845 // replace ".dylib" shared library extension with ".rsrc"
846 theResPath
.Replace(wxT(".dylib"), wxT(".rsrc"));
847 // Find the begining of the filename
848 theName
= theResPath
.AfterLast('/');
851 wxLogDebug( wxT("wxMac resources file name is '%s'"),
852 theResPath
.mb_str() );
855 theErr
= FSPathMakeRef((UInt8
*) theResPath
.mb_str(), &theResRef
, false);
856 if (theErr
!= noErr
) {
857 // try in current directory (using name only)
858 theErr
= FSPathMakeRef((UInt8
*) theName
.mb_str(), &theResRef
, false);
861 // open the resource file
862 if (theErr
== noErr
) {
863 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
864 &gSharedLibraryResource
);
866 if (theErr
!= noErr
) {
868 wxLogDebug( wxT("unable to open wxMac resource file '%s'\n"),
869 theResPath
.mb_str() );
870 #endif // __WXDEBUG__
875 #endif /* __DARWIN__ */
877 #endif /* WXMAKINGDLL_CORE */
880 void wxStAppResource::CloseSharedLibraryResource()
882 #ifdef WXMAKINGDLL_CORE
883 // Close the shared library resource file
884 if (gSharedLibraryResource
!= kResFileNotOpened
) {
886 if (gSharedLibraryBundle
!= NULL
) {
887 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
888 gSharedLibraryResource
);
889 gSharedLibraryBundle
= NULL
;
892 #endif /* __DARWIN__ */
894 CloseResFile(gSharedLibraryResource
);
896 gSharedLibraryResource
= kResFileNotOpened
;
898 #endif /* WXMAKINGDLL_CORE */
901 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
903 // for shared libraries we have to manually get the correct resource
904 // ref num upon initializing and releasing when terminating, therefore
905 // the __wxinitialize and __wxterminate must be used
908 void __sinit(void); /* (generated by linker) */
909 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
910 pascal void __terminate(void);
913 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
915 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
916 return __initialize( theInitBlock
) ;
919 pascal void __wxterminate(void)
921 wxStAppResource::CloseSharedLibraryResource() ;
925 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
929 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
931 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
932 OSStatus err
= noErr
;
935 switch( GetEventClass( event
) )
937 case kEventClassKeyboard
:
940 switch( GetEventKind(event
) )
942 case kEventRawKeyDown
:
943 rec
->what
= keyDown
;
945 case kEventRawKeyRepeat
:
946 rec
->what
= autoKey
;
948 case kEventRawKeyUp
:
951 case kEventRawKeyModifiersChanged
:
952 rec
->what
= nullEvent
;
961 unsigned char charCode
;
963 GetMouse( &rec
->where
) ;
965 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
966 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
967 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
968 rec
->modifiers
= modifiers
;
969 rec
->message
= (keyCode
<< 8 ) + charCode
;
973 case kEventClassTextInput
:
975 switch( GetEventKind( event
) )
977 case kEventTextInputUnicodeForKeyEvent
:
980 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
984 unsigned char charCode
;
986 GetMouse( &rec
->where
) ;
987 rec
->what
= keyDown
;
988 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
989 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
990 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
991 rec
->modifiers
= modifiers
;
992 rec
->message
= (keyCode
<< 8 ) + charCode
;
1008 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1010 OSStatus result = eventNotHandledErr ;
1013 switch ( GetEventClass( event ) )
1015 case kEventClassKeyboard :
1016 if ( wxMacConvertEventToRecord( event , &rec ) )
1018 wxTheApp->MacHandleModifierEvents( &rec ) ;
1019 wxTheApp->MacHandleOneEvent( &rec ) ;
1023 case kEventClassTextInput :
1024 if ( wxMacConvertEventToRecord( event , &rec ) )
1026 wxTheApp->MacHandleModifierEvents( &rec ) ;
1027 wxTheApp->MacHandleOneEvent( &rec ) ;
1041 m_printMode
= wxPRINT_WINDOWS
;
1044 m_macCurrentEvent
= NULL
;
1046 m_macCurrentEventHandlerCallRef
= NULL
;
1050 int wxApp::MainLoop()
1062 void wxApp::ExitMainLoop()
1064 m_keepGoing
= FALSE
;
1067 // Is a message/event pending?
1068 bool wxApp::Pending()
1071 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1075 return EventAvail( everyEvent
, &event
) ;
1079 // Dispatch a message.
1080 bool wxApp::Dispatch()
1087 void wxApp::OnIdle(wxIdleEvent
& event
)
1089 wxAppBase::OnIdle(event
);
1091 // If they are pending events, we must process them: pending events are
1092 // either events to the threads other than main or events posted with
1093 // wxPostEvent() functions
1094 wxMacProcessNotifierAndPendingEvents();
1096 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1097 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1100 void wxApp::WakeUpIdle()
1111 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1114 GetTopWindow()->Close(TRUE
);
1117 // Default behaviour: close the application with prompts. The
1118 // user can veto the close, and therefore the end session.
1119 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1123 if (!GetTopWindow()->Close(!event
.CanVeto()))
1128 extern "C" void wxCYield() ;
1134 // Yield to other processes
1136 bool wxApp::Yield(bool onlyIfNeeded
)
1140 if ( !onlyIfNeeded
)
1142 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1151 YieldToAnyThread() ;
1153 // by definition yield should handle all non-processed events
1157 OSStatus status
= noErr
;
1160 s_inReceiveEvent
= true ;
1161 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1162 s_inReceiveEvent
= false ;
1164 if ( status
== eventLoopTimedOutErr
)
1166 // make sure next time the event loop will trigger idle events
1167 sleepTime
= kEventDurationNoWait
;
1169 else if ( status
== eventLoopQuitErr
)
1171 // according to QA1061 this may also occur when a WakeUp Process
1176 MacHandleOneEvent( theEvent
) ;
1177 ReleaseEvent(theEvent
);
1179 } while( status
== noErr
) ;
1183 // having a larger value here leads to large performance slowdowns
1184 // so we cannot give background apps more processor time here
1185 // we do so however having a large sleep value in the main event loop
1188 while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1190 MacHandleModifierEvents( &event
) ;
1191 MacHandleOneEvent( &event
);
1192 if ( event
.what
!= kHighLevelEvent
)
1193 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1195 MacHandleModifierEvents( &event
) ;
1198 wxMacProcessNotifierAndPendingEvents() ;
1204 // platform specifics
1206 void wxApp::MacSuspend( bool convertClipboard
)
1209 // we have to deactive the top level windows manually
1211 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1214 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1216 #if 0 // having problems right now with that
1217 if (!win
->HasFlag(wxSTAY_ON_TOP
))
1220 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1222 node
= node
->GetNext();
1225 ::HideFloatingWindows() ;
1227 s_lastMouseDown
= 0 ;
1229 if( convertClipboard
)
1231 MacConvertPrivateToPublicScrap() ;
1235 extern wxList wxModalDialogs
;
1237 void wxApp::MacResume( bool convertClipboard
)
1239 s_lastMouseDown
= 0 ;
1240 if( convertClipboard
)
1242 MacConvertPublicToPrivateScrap() ;
1246 ::ShowFloatingWindows() ;
1247 // raise modal dialogs in case a non modal window was selected to activate the app
1249 wxNode
* node
= wxModalDialogs
.GetFirst();
1252 wxDialog
* dialog
= (wxDialog
*) node
->GetData();
1255 node
= node
->GetNext();
1260 void wxApp::MacConvertPrivateToPublicScrap()
1264 void wxApp::MacConvertPublicToPrivateScrap()
1268 void wxApp::MacDoOneEvent()
1273 s_inReceiveEvent
= true ;
1274 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1275 s_inReceiveEvent
= false ;
1276 if ( status
== eventLoopTimedOutErr
)
1278 if ( wxTheApp
->ProcessIdle() )
1279 sleepTime
= kEventDurationNoWait
;
1283 if (g_numberOfThreads
)
1285 sleepTime
= kEventDurationNoWait
;
1288 #endif // wxUSE_THREADS
1290 sleepTime
= kEventDurationSecond
;
1294 else if ( status
== eventLoopQuitErr
)
1296 // according to QA1061 this may also occur when a WakeUp Process
1301 MacHandleOneEvent( theEvent
) ;
1302 ReleaseEvent(theEvent
);
1303 sleepTime
= kEventDurationNoWait
;
1308 EventMask eventMask
= everyEvent
;
1310 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1312 MacHandleModifierEvents( &event
) ;
1313 MacHandleOneEvent( &event
);
1317 MacHandleModifierEvents( &event
) ;
1319 WindowPtr window
= ::FrontWindow() ;
1321 ::IdleControls( window
) ;
1323 if ( wxTheApp
->ProcessIdle() )
1324 sleepTime
= kEventDurationNoWait
;
1328 if (g_numberOfThreads
)
1330 sleepTime
= kEventDurationNoWait
;
1333 #endif // wxUSE_THREADS
1335 sleepTime
= kEventDurationSecond
;
1339 if ( event
.what
!= kHighLevelEvent
)
1340 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1344 DeletePendingObjects() ;
1345 wxMacProcessNotifierAndPendingEvents() ;
1348 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1350 // Override to process unhandled events as you please
1353 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1356 EventTargetRef theTarget
;
1357 theTarget
= GetEventDispatcherTarget();
1358 m_macCurrentEvent
= evr
;
1359 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1360 if(status
== eventNotHandledErr
)
1362 MacHandleUnhandledEvent(evr
);
1365 EventRecord
* ev
= (EventRecord
*) evr
;
1366 m_macCurrentEvent
= ev
;
1368 wxApp::sm_lastMessageTime
= ev
->when
;
1373 MacHandleMouseDownEvent( ev
) ;
1374 if ( ev
->modifiers
& controlKey
)
1375 s_lastMouseDown
= 2;
1377 s_lastMouseDown
= 1;
1380 if ( s_lastMouseDown
== 2 )
1382 ev
->modifiers
|= controlKey
;
1386 ev
->modifiers
&= ~controlKey
;
1388 MacHandleMouseUpEvent( ev
) ;
1389 s_lastMouseDown
= 0;
1392 MacHandleActivateEvent( ev
) ;
1395 // In embedded mode we first let the UnhandledEvent function
1396 // try to handle the update event. If we handle it ourselves
1397 // first and then pass it on, the host's windows won't update.
1398 MacHandleUnhandledEvent(ev
);
1399 MacHandleUpdateEvent( ev
) ;
1403 MacHandleKeyDownEvent( ev
) ;
1406 MacHandleKeyUpEvent( ev
) ;
1409 MacHandleDiskEvent( ev
) ;
1412 MacHandleOSEvent( ev
) ;
1414 case kHighLevelEvent
:
1415 MacHandleHighLevelEvent( ev
) ;
1421 wxMacProcessNotifierAndPendingEvents() ;
1425 bool s_macIsInModalLoop
= false ;
1427 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1429 EventRecord
* ev
= (EventRecord
*) evr
;
1430 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1432 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1434 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1435 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1436 event
.m_altDown
= ev
->modifiers
& optionKey
;
1437 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1439 event
.m_x
= ev
->where
.h
;
1440 event
.m_y
= ev
->where
.v
;
1441 event
.m_timeStamp
= ev
->when
;
1442 wxWindow
* focus
= wxWindow::FindFocus() ;
1443 event
.SetEventObject(focus
);
1445 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1447 event
.m_keyCode
= WXK_CONTROL
;
1448 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1449 focus
->GetEventHandler()->ProcessEvent( event
) ;
1451 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1453 event
.m_keyCode
= WXK_SHIFT
;
1454 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1455 focus
->GetEventHandler()->ProcessEvent( event
) ;
1457 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1459 event
.m_keyCode
= WXK_ALT
;
1460 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1461 focus
->GetEventHandler()->ProcessEvent( event
) ;
1463 if ( ( ev
->modifiers
^ s_lastModifiers
) & cmdKey
)
1465 event
.m_keyCode
= WXK_COMMAND
;
1466 event
.SetEventType( ( ev
->modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1467 focus
->GetEventHandler()->ProcessEvent( event
) ;
1469 s_lastModifiers
= ev
->modifiers
;
1473 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1475 // we must avoid reentrancy problems when processing high level events eg printing
1476 bool former
= s_inYield
;
1478 EventRecord
* ev
= (EventRecord
*) evr
;
1479 ::AEProcessAppleEvent( ev
) ;
1480 s_inYield
= former
;
1483 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1485 EventRecord
* ev
= (EventRecord
*) evr
;
1486 wxToolTip::RemoveToolTips() ;
1489 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1490 WindowAttributes frontWindowAttributes
= NULL
;
1492 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1494 short windowPart
= ::FindWindow(ev
->where
, &window
);
1495 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1496 if ( wxPendingDelete
.Member(win
) )
1500 GetQDGlobalsScreenBits( &screenBits
);
1505 if ( s_macIsInModalLoop
)
1511 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1512 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1513 s_lastMouseDown
= 0;
1517 SystemClick( ev
, window
) ;
1518 s_lastMouseDown
= 0;
1521 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1527 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1532 Point pt
= { 0, 0 } ;
1533 SetPortWindowPort(window
) ;
1534 LocalToGlobal( &pt
) ;
1536 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1537 -1 , wxSIZE_USE_EXISTING
);
1539 s_lastMouseDown
= 0;
1543 if (TrackGoAway(window
, ev
->where
))
1548 s_lastMouseDown
= 0;
1552 Rect newContentRect
;
1553 Rect constraintRect
;
1554 constraintRect
.top
= win
->GetMinHeight() ;
1555 if ( constraintRect
.top
== -1 )
1556 constraintRect
.top
= 0 ;
1557 constraintRect
.left
= win
->GetMinWidth() ;
1558 if ( constraintRect
.left
== -1 )
1559 constraintRect
.left
= 0 ;
1560 constraintRect
.right
= win
->GetMaxWidth() ;
1561 if ( constraintRect
.right
== -1 )
1562 constraintRect
.right
= 32000 ;
1563 constraintRect
.bottom
= win
->GetMaxHeight() ;
1564 if ( constraintRect
.bottom
== -1 )
1565 constraintRect
.bottom
= 32000 ;
1567 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1568 &constraintRect
, &newContentRect
) ;
1571 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1572 newContentRect
.right
- newContentRect
.left
,
1573 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1575 s_lastMouseDown
= 0;
1580 if (TrackBox(window
, ev
->where
, windowPart
))
1582 // TODO setup size event
1583 ZoomWindow( window
, windowPart
, false ) ;
1589 Point pt
= { 0, 0 } ;
1590 SetPortWindowPort(window
) ;
1591 LocalToGlobal( &pt
) ;
1594 GetWindowPortBounds(window
, &tempRect
) ;
1595 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1596 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1599 s_lastMouseDown
= 0;
1601 case inCollapseBox
:
1602 // TODO setup size event
1603 s_lastMouseDown
= 0;
1610 SetPortWindowPort(window
) ;
1613 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1615 if ( s_macIsInModalLoop
)
1619 else if ( UMAIsWindowFloating( window
) )
1622 win
->MacMouseDown( ev
, windowPart
) ;
1626 // Activate window first
1627 ::SelectWindow( window
) ;
1631 win
->MacMouseDown( ev
, windowPart
) ;
1637 win
->MacMouseDown( ev
, windowPart
) ;
1645 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1647 EventRecord
* ev
= (EventRecord
*) evr
;
1650 short windowPart
= inNoWindow
;
1651 if ( wxTheApp
->s_captureWindow
)
1653 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1654 windowPart
= inContent
;
1658 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1669 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1671 win
->MacMouseUp( ev
, windowPart
) ;
1679 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1680 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1685 case kHomeCharCode
:
1688 case kEnterCharCode
:
1689 retval
= WXK_RETURN
;
1694 case kHelpCharCode
:
1697 case kBackspaceCharCode
:
1703 case kPageUpCharCode
:
1704 retval
= WXK_PAGEUP
;
1706 case kPageDownCharCode
:
1707 retval
= WXK_PAGEDOWN
;
1709 case kReturnCharCode
:
1710 retval
= WXK_RETURN
;
1712 case kFunctionKeyCharCode
:
1764 case kEscapeCharCode
:
1765 retval
= WXK_ESCAPE
;
1767 case kLeftArrowCharCode
:
1770 case kRightArrowCharCode
:
1771 retval
= WXK_RIGHT
;
1773 case kUpArrowCharCode
:
1776 case kDownArrowCharCode
:
1779 case kDeleteCharCode
:
1780 retval
= WXK_DELETE
;
1788 int wxKeyCodeToMacModifier(wxKeyCode key
)
1813 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1815 // TODO: Have it use HID Manager on OSX...
1816 //if OS X > 10.2 (i.e. 10.2.x)
1817 //a known apple bug prevents the system from determining led
1818 //states with GetKeys... can only determine caps lock led
1819 return !!(GetCurrentKeyModifiers() & wxKeyCodeToMacModifier(key
));
1821 // KeyMapByteArray keymap;
1822 // GetKeys((BigEndianLong*)keymap);
1823 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1827 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1829 EventRecord
* ev
= (EventRecord
*) evr
;
1830 wxToolTip::RemoveToolTips() ;
1832 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1833 if ( HiWord( menuresult
) )
1835 if ( !s_macIsInModalLoop
)
1836 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1840 wxWindow
* focus
= wxWindow::FindFocus() ;
1842 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1845 // we must handle control keys the other way round, otherwise text content is updated too late
1846 // has not been handled -> perform default
1847 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1848 if ( control
&& control
->GetMacControl() != NULL
)
1852 keychar
= short(ev
->message
& charCodeMask
);
1853 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1854 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1861 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1863 EventRecord
* ev
= (EventRecord
*) evr
;
1864 wxToolTip::RemoveToolTips() ;
1866 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1867 if ( HiWord( menuresult
) )
1872 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1878 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1885 keychar
= short(keymessage
& charCodeMask
);
1886 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1888 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1890 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1891 // and look at the character after
1893 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1894 keychar
= short(keyInfo
& charCodeMask
);
1895 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1897 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1898 long realkeyval
= keyval
;
1899 if ( keyval
== keychar
)
1901 // 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)
1902 realkeyval
= short(keymessage
& charCodeMask
) ;
1903 keyval
= wxToupper( keyval
) ;
1906 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1907 bool handled
= false ;
1908 event
.m_shiftDown
= modifiers
& shiftKey
;
1909 event
.m_controlDown
= modifiers
& controlKey
;
1910 event
.m_altDown
= modifiers
& optionKey
;
1911 event
.m_metaDown
= modifiers
& cmdKey
;
1912 event
.m_keyCode
= keyval
;
1916 event
.m_timeStamp
= when
;
1917 event
.SetEventObject(focus
);
1918 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1919 if ( handled
&& event
.GetSkipped() )
1926 wxWindow
*ancestor
= focus
;
1929 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1932 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1933 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1936 if (ancestor
->IsTopLevel())
1938 ancestor
= ancestor
->GetParent();
1941 #endif // wxUSE_ACCEL
1945 event
.Skip( FALSE
) ;
1946 event
.SetEventType( wxEVT_CHAR
) ;
1948 event
.m_keyCode
= realkeyval
;
1950 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1951 if ( handled
&& event
.GetSkipped() )
1955 (keyval
== WXK_TAB
) &&
1956 // CS: copied the change below from wxGTK
1957 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1958 // have this style, yet choose not to process this particular TAB in which
1959 // case TAB must still work as a navigational character
1961 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
1963 (focus
->GetParent()) &&
1964 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
1966 wxNavigationKeyEvent new_event
;
1967 new_event
.SetEventObject( focus
);
1968 new_event
.SetDirection( !event
.ShiftDown() );
1969 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1970 new_event
.SetWindowChange( event
.ControlDown() );
1971 new_event
.SetCurrentFocus( focus
);
1972 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1973 if ( handled
&& new_event
.GetSkipped() )
1976 // backdoor handler for default return and command escape
1977 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1979 // if window is not having a focus still testing for default enter or cancel
1980 // TODO add the UMA version for ActiveNonFloatingWindow
1981 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1984 if ( keyval
== WXK_RETURN
)
1986 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1988 if ( def
&& def
->IsEnabled() )
1990 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1991 event
.SetEventObject(def
);
1992 def
->Command(event
);
1996 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1997 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1999 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
2000 new_event
.SetEventObject( focus
);
2001 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2008 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
2015 keychar
= short(keymessage
& charCodeMask
);
2016 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
2017 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
2019 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2020 // and look at the character after
2022 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2023 keychar
= short(keyInfo
& charCodeMask
);
2024 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2026 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2028 if ( keyval
== keychar
)
2030 keyval
= wxToupper( keyval
) ;
2032 bool handled
= false ;
2034 wxKeyEvent
event(wxEVT_KEY_UP
);
2035 event
.m_shiftDown
= modifiers
& shiftKey
;
2036 event
.m_controlDown
= modifiers
& controlKey
;
2037 event
.m_altDown
= modifiers
& optionKey
;
2038 event
.m_metaDown
= modifiers
& cmdKey
;
2039 event
.m_keyCode
= keyval
;
2043 event
.m_timeStamp
= when
;
2044 event
.SetEventObject(focus
);
2045 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2051 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
2053 EventRecord
* ev
= (EventRecord
*) evr
;
2054 WindowRef window
= (WindowRef
) ev
->message
;
2057 bool activate
= (ev
->modifiers
& activeFlag
) ;
2058 WindowClass wclass
;
2059 ::GetWindowClass ( window
, &wclass
) ;
2060 if ( wclass
== kFloatingWindowClass
)
2062 // if it is a floater we activate/deactivate the front non-floating window instead
2063 window
= ::FrontNonFloatingWindow() ;
2065 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2067 win
->MacActivate( ev
->when
, activate
) ;
2071 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
2073 EventRecord
* ev
= (EventRecord
*) evr
;
2074 WindowRef window
= (WindowRef
) ev
->message
;
2075 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2078 if ( !wxPendingDelete
.Member(win
) )
2079 win
->MacUpdate( ev
->when
) ;
2083 // since there is no way of telling this foreign window to update itself
2084 // we have to invalidate the update region otherwise we keep getting the same
2085 // event over and over again
2086 BeginUpdate( window
) ;
2087 EndUpdate( window
) ;
2091 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
2093 EventRecord
* ev
= (EventRecord
*) evr
;
2094 if ( HiWord( ev
->message
) != noErr
)
2098 SetPt( &point
, 100 , 100 ) ;
2100 err
= DIBadMount( point
, ev
->message
) ;
2101 wxASSERT( err
== noErr
) ;
2105 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2107 EventRecord
* ev
= (EventRecord
*) evr
;
2108 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2110 case suspendResumeMessage
:
2112 bool isResuming
= ev
->message
& resumeFlag
;
2113 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2115 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2118 WindowRef oldFrontWindow
= NULL
;
2119 WindowRef newFrontWindow
= NULL
;
2121 // in case we don't take care of activating ourselves, we have to synchronize
2122 // our idea of the active window with the process manager's - which it already activated
2124 if ( !doesActivate
)
2125 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2127 MacResume( convertClipboard
) ;
2129 newFrontWindow
= ::FrontNonFloatingWindow() ;
2131 if ( oldFrontWindow
)
2133 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2135 win
->MacActivate( ev
->when
, false ) ;
2137 if ( newFrontWindow
)
2139 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2141 win
->MacActivate( ev
->when
, true ) ;
2146 MacSuspend( convertClipboard
) ;
2150 case mouseMovedMessage
:
2154 wxWindow
* currentMouseWindow
= NULL
;
2156 if (s_captureWindow
)
2158 currentMouseWindow
= s_captureWindow
;
2162 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2163 ¤tMouseWindow
) ;
2166 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2168 wxMouseEvent event
;
2170 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2171 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2173 event
.m_leftDown
= isDown
&& !controlDown
;
2174 event
.m_middleDown
= FALSE
;
2175 event
.m_rightDown
= isDown
&& controlDown
;
2176 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2177 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2178 event
.m_altDown
= ev
->modifiers
& optionKey
;
2179 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2180 event
.m_x
= ev
->where
.h
;
2181 event
.m_y
= ev
->where
.v
;
2182 event
.m_timeStamp
= ev
->when
;
2183 event
.SetEventObject(this);
2185 if ( wxWindow::s_lastMouseWindow
)
2187 wxMouseEvent
eventleave(event
);
2188 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2189 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2190 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2192 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2194 if ( currentMouseWindow
)
2196 wxMouseEvent
evententer(event
);
2197 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2198 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2199 evententer
.SetEventObject( currentMouseWindow
) ;
2200 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2202 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2205 short windowPart
= inNoWindow
;
2207 if ( s_captureWindow
)
2209 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2210 windowPart
= inContent
;
2214 windowPart
= ::FindWindow(ev
->where
, &window
);
2221 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2223 win
->MacMouseMoved( ev
, windowPart
) ;
2230 UMAShowArrowCursor();
2240 UMAShowArrowCursor();
2251 void wxApp::MacHandleMouseMovedEvent(wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
2255 wxWindow
* currentMouseWindow
= NULL
;
2257 if (s_captureWindow
)
2259 currentMouseWindow
= s_captureWindow
;
2263 wxWindow::MacGetWindowFromPoint( wxPoint( x
, y
) , ¤tMouseWindow
) ;
2266 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2268 wxMouseEvent event
;
2270 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
2271 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
2273 event
.m_leftDown
= isDown
&& !controlDown
;
2275 event
.m_middleDown
= FALSE
;
2276 event
.m_rightDown
= isDown
&& controlDown
;
2278 event
.m_shiftDown
= modifiers
& shiftKey
;
2279 event
.m_controlDown
= modifiers
& controlKey
;
2280 event
.m_altDown
= modifiers
& optionKey
;
2281 event
.m_metaDown
= modifiers
& cmdKey
;
2285 event
.m_timeStamp
= timestamp
;
2287 if ( wxWindow::s_lastMouseWindow
)
2289 wxMouseEvent
eventleave(event
);
2290 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2291 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2292 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2295 wxToolTip::RelayEvent( wxWindow::s_lastMouseWindow
, eventleave
);
2296 #endif // wxUSE_TOOLTIPS
2297 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2299 if ( currentMouseWindow
)
2301 wxMouseEvent
evententer(event
);
2302 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2303 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2304 evententer
.SetEventObject( currentMouseWindow
) ;
2306 wxToolTip::RelayEvent( currentMouseWindow
, evententer
);
2307 #endif // wxUSE_TOOLTIPS
2308 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2310 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2313 short windowPart
= inNoWindow
;
2315 if ( s_captureWindow
)
2317 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2318 windowPart
= inContent
;
2322 Point pt
= { y
, x
} ;
2323 windowPart
= ::FindWindow(pt
, &window
);
2330 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2332 win
->MacFireMouseEvent( nullEvent
, x
, y
, modifiers
, timestamp
) ;
2339 UMAShowArrowCursor();
2349 UMAShowArrowCursor();
2356 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2358 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2359 wxFrame
* frame
= mbar
->GetFrame();
2360 wxCHECK_RET( mbar
!= NULL
&& frame
!= NULL
, wxT("error in menu item callback") );
2363 frame
->ProcessCommand(id
);
2368 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2371 return; // no menu item selected
2373 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2376 Str255 deskAccessoryName
;
2379 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2380 GetPort(&savedPort
);
2381 OpenDeskAcc(deskAccessoryName
);
2388 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2389 MacHandleMenuCommand( id
) ;