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"
40 #include "wx/filename.h"
54 #include "wx/mac/uma.h"
55 #include "wx/mac/macnotfy.h"
58 # include <CoreServices/CoreServices.h>
59 # if defined(WXMAKINGDLL_CORE)
60 # include <mach-o/dyld.h>
65 # include <ToolUtils.h>
66 # include <DiskInit.h>
70 extern wxList wxPendingDelete
;
71 extern wxList
*wxWinMacWindowList
;
72 extern wxList
*wxWinMacControlList
;
74 extern size_t g_numberOfThreads
;
75 #endif // wxUSE_THREADS
77 // statics for implementation
79 static bool s_inYield
= FALSE
;
82 static bool s_inReceiveEvent
= FALSE
;
83 static EventTime sleepTime
= kEventDurationNoWait
;
85 static long sleepTime
= 0 ;
88 #if !USE_SHARED_LIBRARY
89 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
90 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
91 EVT_IDLE(wxApp::OnIdle
)
92 EVT_END_SESSION(wxApp::OnEndSession
)
93 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
98 const short kMacMinHeap
= (29 * 1024) ;
99 // platform specific static variables
101 const short kwxMacMenuBarResource
= 1 ;
102 const short kwxMacAppleMenuId
= 1 ;
104 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
105 wxWindow
* wxApp::s_captureWindow
= NULL
;
106 int wxApp::s_lastMouseDown
= 0 ;
107 long wxApp::sm_lastMessageTime
= 0;
108 long wxApp::s_lastModifiers
= 0 ;
111 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
112 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
113 long wxApp::s_macPreferencesMenuItemId
= wxID_PREFERENCES
;
114 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
115 wxString
wxApp::s_macHelpMenuTitleName
= wxT("&Help") ;
117 // Normally we're not a plugin
118 bool wxApp::sm_isEmbedded
= false;
119 //----------------------------------------------------------------------
120 // Core Apple Event Support
121 //----------------------------------------------------------------------
123 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
124 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
125 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
126 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
127 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
129 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
131 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
134 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
136 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
139 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
141 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
144 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
146 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
149 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
151 return wxTheApp
->MacHandleAERApp( (AppleEvent
*) event
, reply
) ;
154 // AEODoc Calls MacOpenFile on each of the files passed
156 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
160 DescType returnedType
;
166 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
170 err
= AECountItems(&docList
, &itemsInList
);
174 ProcessSerialNumber PSN
;
175 PSN
.highLongOfPSN
= 0 ;
176 PSN
.lowLongOfPSN
= kCurrentProcess
;
177 SetFrontProcess( &PSN
) ;
179 for (i
= 1; i
<= itemsInList
; i
++) {
180 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
181 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
182 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
188 // AEPDoc Calls MacPrintFile on each of the files passed
190 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
194 DescType returnedType
;
200 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
204 err
= AECountItems(&docList
, &itemsInList
);
208 ProcessSerialNumber PSN
;
209 PSN
.highLongOfPSN
= 0 ;
210 PSN
.lowLongOfPSN
= kCurrentProcess
;
211 SetFrontProcess( &PSN
) ;
213 for (i
= 1; i
<= itemsInList
; i
++) {
214 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
215 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
216 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
222 // AEOApp calls MacNewFile
224 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
230 // AEQuit attempts to quit the application
232 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
234 wxWindow
* win
= GetTopWindow() ;
246 // AEROApp calls MacReopenApp
248 short wxApp::MacHandleAERApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
255 //----------------------------------------------------------------------
256 // Support Routines linking the Mac...File Calls to the Document Manager
257 //----------------------------------------------------------------------
259 void wxApp::MacOpenFile(const wxString
& fileName
)
261 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
263 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
266 void wxApp::MacPrintFile(const wxString
& fileName
)
268 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
271 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
274 wxView
* view
= doc
->GetFirstView() ;
277 wxPrintout
*printout
= view
->OnCreatePrintout();
281 printer
.Print(view
->GetFrame(), printout
, TRUE
);
287 doc
->DeleteAllViews();
288 dm
->RemoveDocument(doc
) ;
294 void wxApp::MacNewFile()
298 void wxApp::MacReopenApp()
300 // eventually check for open docs, if none, call MacNewFile
303 //----------------------------------------------------------------------
304 // Carbon Event Handler
305 //----------------------------------------------------------------------
309 static const EventTypeSpec eventList
[] =
311 { kEventClassCommand
, kEventProcessCommand
} ,
312 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
314 { kEventClassMenu
, kEventMenuOpening
},
315 { kEventClassMenu
, kEventMenuClosed
},
316 { kEventClassMenu
, kEventMenuTargetItem
},
318 { kEventClassApplication
, kEventAppActivated
} ,
319 { kEventClassApplication
, kEventAppDeactivated
} ,
320 // handling the quit event is not recommended by apple
321 // rather using the quit apple event - which we do
323 { kEventClassAppleEvent
, kEventAppleEvent
} ,
325 { kEventClassMouse
, kEventMouseDown
} ,
326 { kEventClassMouse
, kEventMouseMoved
} ,
327 { kEventClassMouse
, kEventMouseUp
} ,
328 { kEventClassMouse
, kEventMouseDragged
} ,
332 static pascal OSStatus
333 MenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
335 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar();
336 wxFrame
* win
= mbar
->GetFrame();
340 // VZ: we could find the menu from its handle here by examining all
341 // the menus in the menu bar recursively but knowing that neither
342 // wxMSW nor wxGTK do it why bother...
346 GetEventParameter(event
,
347 kEventParamDirectObject
,
349 sizeof(menuRef
), NULL
,
355 switch (GetEventKind(event
))
357 case kEventMenuOpening
:
358 type
= wxEVT_MENU_OPEN
;
360 case kEventMenuClosed
:
361 type
= wxEVT_MENU_CLOSE
;
363 case kEventMenuTargetItem
:
364 type
= wxEVT_MENU_HIGHLIGHT
;
365 GetEventParameter(event
, kEventParamMenuCommand
,
366 typeMenuCommand
, NULL
,
367 sizeof(cmd
), NULL
, &cmd
);
368 if (cmd
== 0) return eventNotHandledErr
;
371 wxFAIL_MSG(wxT("Unexpected menu event kind"));
375 wxMenuEvent
wxevent(type
, cmd
);
376 wxevent
.SetEventObject(win
);
378 (void)win
->GetEventHandler()->ProcessEvent(wxevent
);
381 return eventNotHandledErr
;
384 // due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop
385 // but have to use ReceiveNextEvent dealing with events manually, therefore we also have
386 // deal with clicks in the menu bar explicitely
388 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
) ;
390 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
392 OSStatus result
= eventNotHandledErr
;
395 UInt32 modifiers
= 0;
396 EventMouseButton button
= 0 ;
399 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
400 sizeof( Point
), NULL
, &point
);
401 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
,
402 sizeof( UInt32
), NULL
, &modifiers
);
403 GetEventParameter( event
, kEventParamMouseButton
, typeMouseButton
, NULL
,
404 sizeof( EventMouseButton
), NULL
, &button
);
405 GetEventParameter( event
, kEventParamClickCount
, typeUInt32
, NULL
,
406 sizeof( UInt32
), NULL
, &click
);
408 if ( button
== 0 || GetEventKind( event
) == kEventMouseUp
)
409 modifiers
+= btnState
;
412 switch( GetEventKind(event
) )
414 case kEventMouseDown
:
418 short windowPart
= ::FindWindow(point
, &window
);
420 if ( windowPart
== inMenuBar
)
422 MenuSelect( point
) ;
427 case kEventMouseDragged
:
430 if ( wxTheApp
->s_captureWindow
)
431 wxMacWindowEventHandler( handler
, event
, (void*) wxTheApp
->s_captureWindow
->MacGetTopLevelWindow() ) ;
434 case kEventMouseMoved
:
436 wxTheApp
->MacHandleMouseMovedEvent( point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
446 static pascal OSStatus
CommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
448 OSStatus result
= eventNotHandledErr
;
452 GetEventParameter( event
, kEventParamDirectObject
, typeHICommand
, NULL
,
453 sizeof( HICommand
), NULL
, &command
);
455 MenuCommand id
= command
.commandID
;
456 if ( id
== kHICommandPreferences
)
457 id
= wxApp::s_macPreferencesMenuItemId
;
459 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
460 wxMenu
* menu
= NULL
;
461 wxMenuItem
* item
= NULL
;
465 item
= mbar
->FindItem( id
, &menu
) ;
466 // it is not 100 % sure that an menu of id 0 is really ours, safety check
467 if ( id
== 0 && menu
!= NULL
&& menu
->GetHMenu() != command
.menu
.menuRef
)
474 if ( item
== NULL
|| menu
== NULL
|| mbar
== NULL
)
477 switch( GetEventKind( event
) )
479 case kEventProcessCommand
:
481 if (item
->IsCheckable())
483 item
->Check( !item
->IsChecked() ) ;
486 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
490 case kEventCommandUpdateStatus
:
491 // eventually trigger an updateui round
501 static pascal OSStatus
ApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
503 OSStatus result
= eventNotHandledErr
;
504 switch ( GetEventKind( event
) )
506 case kEventAppActivated
:
509 wxTheApp
->MacResume( true ) ;
513 case kEventAppDeactivated
:
516 wxTheApp
->MacSuspend( true ) ;
526 pascal OSStatus
wxAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
528 OSStatus result
= eventNotHandledErr
;
529 switch( GetEventClass( event
) )
531 case kEventClassCommand
:
532 result
= CommandEventHandler( handler
, event
, data
) ;
534 case kEventClassApplication
:
535 result
= ApplicationEventHandler( handler
, event
, data
) ;
537 case kEventClassMenu
:
538 result
= MenuEventHandler( handler
, event
, data
) ;
540 case kEventClassMouse
:
541 result
= MouseEventHandler( handler
, event
, data
) ;
543 case kEventClassAppleEvent
:
546 wxMacConvertEventToRecord( event
, &rec
) ;
547 result
= AEProcessAppleEvent( &rec
) ;
557 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler
)
561 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
562 // we know it's there ;-)
563 WXIMPORT
char std::__throws_bad_alloc
;
566 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
572 UMAInitToolbox( 4, sm_isEmbedded
) ;
573 SetEventMask( everyEvent
) ;
574 UMAShowWatchCursor() ;
576 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
577 // open shared library resources from here since we don't have
578 // __wxinitialize in Mach-O shared libraries
579 wxStAppResource::OpenSharedLibraryResource(NULL
);
583 // test the minimal configuration necessary
589 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
591 error
= kMacSTRWrongMachine
;
593 else if (theMachine
< gestaltMacPlus
)
595 error
= kMacSTRWrongMachine
;
597 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
599 error
= kMacSTROldSystem
;
601 else if ( theSystem
< 0x0860 )
603 error
= kMacSTROldSystem
;
605 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
607 error
= kMacSTRSmallSize
;
613 if ( !UMAHasAppearance() )
615 error = kMacSTRNoPre8Yet ;
621 // if we encountered any problems so far, give the error code and exit immediately
625 wxStAppResource resload
;
629 GetIndString(message
, 128, error
);
630 UMAShowArrowCursor() ;
631 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
632 itemHit
= Alert(128, nil
);
637 # if __option(profile)
638 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
643 // now avoid exceptions thrown for new (bad_alloc)
644 // FIXME CS for some changes outside wxMac does not compile anymore
646 std::__throws_bad_alloc
= 0 ;
651 s_macCursorRgn
= ::NewRgn() ;
653 // Mac OS X passes a process serial number command line argument when
654 // the application is launched from the Finder. This argument must be
655 // removed from the command line arguments before being handled by the
656 // application (otherwise applications would need to handle it)
659 static const wxChar
*ARG_PSN
= _T("-psn_");
660 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
662 // remove this argument
664 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
668 if ( !wxAppBase::Initialize(argc
, argv
) )
672 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
676 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
677 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
679 wxMacCreateNotifierTable() ;
681 UMAShowArrowCursor() ;
686 bool wxApp::OnInitGui()
688 if( !wxAppBase::OnInitGui() )
692 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
696 InstallApplicationEventHandler(
697 GetwxAppEventHandlerUPP(),
698 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
704 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
705 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
706 NewAEEventHandlerUPP(AEHandleODoc
) ,
708 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
709 NewAEEventHandlerUPP(AEHandleOApp
) ,
711 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
712 NewAEEventHandlerUPP(AEHandlePDoc
) ,
714 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
715 NewAEEventHandlerUPP(AEHandleRApp
) ,
717 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
718 NewAEEventHandlerUPP(AEHandleQuit
) ,
721 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
722 NewAEEventHandlerProc(AEHandleODoc
) ,
724 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
725 NewAEEventHandlerProc(AEHandleOApp
) ,
727 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
728 NewAEEventHandlerProc(AEHandlePDoc
) ,
730 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
731 NewAEEventHandlerProc(AEHandleRApp
) ,
733 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
734 NewAEEventHandlerProc(AEHandleQuit
) ,
742 void wxApp::CleanUp()
744 wxToolTip::RemoveToolTips() ;
746 // One last chance for pending objects to be cleaned up
747 wxTheApp
->DeletePendingObjects();
749 wxMacDestroyNotifierTable() ;
751 delete wxWinMacWindowList
;
752 wxWinMacWindowList
= NULL
;
754 delete wxWinMacControlList
;
755 wxWinMacControlList
= NULL
;
758 # if __option(profile)
759 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
764 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
765 // close shared library resources from here since we don't have
766 // __wxterminate in Mach-O shared libraries
767 wxStAppResource::CloseSharedLibraryResource();
770 UMACleanupToolbox() ;
771 if (s_macCursorRgn
) {
772 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
779 wxAppBase::CleanUp();
782 //----------------------------------------------------------------------
783 // misc initialization stuff
784 //----------------------------------------------------------------------
786 // extern variable for shared library resource id
787 // need to be able to find it with NSLookupAndBindSymbol
788 short gSharedLibraryResource
= kResFileNotOpened
;
790 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
791 CFBundleRef gSharedLibraryBundle
= NULL
;
792 #endif /* WXMAKINGDLL_CORE && __DARWIN__ */
794 wxStAppResource::wxStAppResource()
796 m_currentRefNum
= CurResFile() ;
797 if ( gSharedLibraryResource
!= kResFileNotOpened
)
799 UseResFile( gSharedLibraryResource
) ;
803 wxStAppResource::~wxStAppResource()
805 if ( m_currentRefNum
!= kResFileNotOpened
)
807 UseResFile( m_currentRefNum
) ;
811 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
813 gSharedLibraryResource
= kResFileNotOpened
;
815 #ifdef WXMAKINGDLL_CORE
816 if ( initBlock
!= NULL
) {
817 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
818 FSSpec
*fileSpec
= NULL
;
820 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
821 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
823 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
824 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
827 if (fileSpec
!= NULL
) {
828 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
833 // Open the shared library resource file if it is not yet open
836 const char *theLibPath
;
838 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
839 if (gSharedLibraryBundle
!= NULL
) {
840 // wxWindows has been bundled into a framework
841 // load the framework resources
843 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
846 // wxWindows is a simple dynamic shared library
847 // load the resources from the data fork of a separate resource file
851 OSErr theErr
= noErr
;
853 // get the library path
854 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
855 theModule
= NSModuleForSymbol(theSymbol
);
856 theLibPath
= NSLibraryNameForModule(theModule
);
858 // if we call wxLogDebug from here then, as wxTheApp hasn't been
859 // created yet when we're called from wxApp::Initialize(), wxLog
860 // is going to create a default stderr-based log target instead of
861 // the expected normal GUI one -- don't do it, if we really want
862 // to see this message just use fprintf() here
864 wxLogDebug( wxT("wxMac library installation name is '%s'"),
868 // allocate copy to replace .dylib.* extension with .rsrc
869 if (theLibPath
!= NULL
) {
871 theResPath
= wxString(theLibPath
, wxConvLocal
);
873 theResPath
= wxString(theLibPath
);
875 // replace '_core' with '' in case of multi-lib build
876 theResPath
.Replace(wxT("_core"), wxEmptyString
);
877 // replace ".dylib" shared library extension with ".rsrc"
878 theResPath
.Replace(wxT(".dylib"), wxT(".rsrc"));
879 // Find the begining of the filename
880 theName
= theResPath
.AfterLast('/');
883 wxLogDebug( wxT("wxMac resources file name is '%s'"),
884 theResPath
.mb_str() );
887 theErr
= FSPathMakeRef((UInt8
*) theResPath
.mb_str(), &theResRef
, false);
888 if (theErr
!= noErr
) {
889 // try in current directory (using name only)
890 theErr
= FSPathMakeRef((UInt8
*) theName
.mb_str(), &theResRef
, false);
893 // open the resource file
894 if (theErr
== noErr
) {
895 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
896 &gSharedLibraryResource
);
898 if (theErr
!= noErr
) {
900 wxLogDebug( wxT("unable to open wxMac resource file '%s'\n"),
901 theResPath
.mb_str() );
902 #endif // __WXDEBUG__
907 #endif /* __DARWIN__ */
909 #endif /* WXMAKINGDLL_CORE */
912 void wxStAppResource::CloseSharedLibraryResource()
914 #ifdef WXMAKINGDLL_CORE
915 // Close the shared library resource file
916 if (gSharedLibraryResource
!= kResFileNotOpened
) {
918 if (gSharedLibraryBundle
!= NULL
) {
919 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
920 gSharedLibraryResource
);
921 gSharedLibraryBundle
= NULL
;
924 #endif /* __DARWIN__ */
926 CloseResFile(gSharedLibraryResource
);
928 gSharedLibraryResource
= kResFileNotOpened
;
930 #endif /* WXMAKINGDLL_CORE */
933 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
935 // for shared libraries we have to manually get the correct resource
936 // ref num upon initializing and releasing when terminating, therefore
937 // the __wxinitialize and __wxterminate must be used
940 void __sinit(void); /* (generated by linker) */
941 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
942 pascal void __terminate(void);
945 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
947 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
948 return __initialize( theInitBlock
) ;
951 pascal void __wxterminate(void)
953 wxStAppResource::CloseSharedLibraryResource() ;
957 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
961 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
963 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
964 OSStatus err
= noErr
;
967 switch( GetEventClass( event
) )
969 case kEventClassKeyboard
:
972 switch( GetEventKind(event
) )
974 case kEventRawKeyDown
:
975 rec
->what
= keyDown
;
977 case kEventRawKeyRepeat
:
978 rec
->what
= autoKey
;
980 case kEventRawKeyUp
:
983 case kEventRawKeyModifiersChanged
:
984 rec
->what
= nullEvent
;
993 unsigned char charCode
;
995 GetMouse( &rec
->where
) ;
997 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
998 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
999 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1000 rec
->modifiers
= modifiers
;
1001 rec
->message
= (keyCode
<< 8 ) + charCode
;
1005 case kEventClassTextInput
:
1007 switch( GetEventKind( event
) )
1009 case kEventTextInputUnicodeForKeyEvent
:
1012 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
1016 unsigned char charCode
;
1018 GetMouse( &rec
->where
) ;
1019 rec
->what
= keyDown
;
1020 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1021 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1022 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1023 rec
->modifiers
= modifiers
;
1024 rec
->message
= (keyCode
<< 8 ) + charCode
;
1040 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1042 OSStatus result = eventNotHandledErr ;
1045 switch ( GetEventClass( event ) )
1047 case kEventClassKeyboard :
1048 if ( wxMacConvertEventToRecord( event , &rec ) )
1050 wxTheApp->MacHandleModifierEvents( &rec ) ;
1051 wxTheApp->MacHandleOneEvent( &rec ) ;
1055 case kEventClassTextInput :
1056 if ( wxMacConvertEventToRecord( event , &rec ) )
1058 wxTheApp->MacHandleModifierEvents( &rec ) ;
1059 wxTheApp->MacHandleOneEvent( &rec ) ;
1073 m_printMode
= wxPRINT_WINDOWS
;
1076 m_macCurrentEvent
= NULL
;
1078 m_macCurrentEventHandlerCallRef
= NULL
;
1082 int wxApp::MainLoop()
1094 void wxApp::ExitMainLoop()
1096 m_keepGoing
= FALSE
;
1099 // Is a message/event pending?
1100 bool wxApp::Pending()
1103 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1107 return EventAvail( everyEvent
, &event
) ;
1111 // Dispatch a message.
1112 bool wxApp::Dispatch()
1119 void wxApp::OnIdle(wxIdleEvent
& event
)
1121 wxAppBase::OnIdle(event
);
1123 // If they are pending events, we must process them: pending events are
1124 // either events to the threads other than main or events posted with
1125 // wxPostEvent() functions
1126 wxMacProcessNotifierAndPendingEvents();
1128 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1129 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1132 void wxApp::WakeUpIdle()
1143 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1146 GetTopWindow()->Close(TRUE
);
1149 // Default behaviour: close the application with prompts. The
1150 // user can veto the close, and therefore the end session.
1151 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1155 if (!GetTopWindow()->Close(!event
.CanVeto()))
1160 extern "C" void wxCYield() ;
1166 // Yield to other processes
1168 bool wxApp::Yield(bool onlyIfNeeded
)
1172 if ( !onlyIfNeeded
)
1174 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1183 YieldToAnyThread() ;
1185 // by definition yield should handle all non-processed events
1189 OSStatus status
= noErr
;
1192 s_inReceiveEvent
= true ;
1193 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1194 s_inReceiveEvent
= false ;
1196 if ( status
== eventLoopTimedOutErr
)
1198 // make sure next time the event loop will trigger idle events
1199 sleepTime
= kEventDurationNoWait
;
1201 else if ( status
== eventLoopQuitErr
)
1203 // according to QA1061 this may also occur when a WakeUp Process
1208 MacHandleOneEvent( theEvent
) ;
1209 ReleaseEvent(theEvent
);
1211 } while( status
== noErr
) ;
1215 // having a larger value here leads to large performance slowdowns
1216 // so we cannot give background apps more processor time here
1217 // we do so however having a large sleep value in the main event loop
1220 while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1222 MacHandleModifierEvents( &event
) ;
1223 MacHandleOneEvent( &event
);
1224 if ( event
.what
!= kHighLevelEvent
)
1225 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1227 MacHandleModifierEvents( &event
) ;
1230 wxMacProcessNotifierAndPendingEvents() ;
1236 // platform specifics
1238 void wxApp::MacSuspend( bool convertClipboard
)
1241 // we have to deactive the top level windows manually
1243 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1246 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1248 #if 0 // having problems right now with that
1249 if (!win
->HasFlag(wxSTAY_ON_TOP
))
1252 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1254 node
= node
->GetNext();
1257 ::HideFloatingWindows() ;
1259 s_lastMouseDown
= 0 ;
1261 if( convertClipboard
)
1263 MacConvertPrivateToPublicScrap() ;
1267 extern wxList wxModalDialogs
;
1269 void wxApp::MacResume( bool convertClipboard
)
1271 s_lastMouseDown
= 0 ;
1272 if( convertClipboard
)
1274 MacConvertPublicToPrivateScrap() ;
1278 ::ShowFloatingWindows() ;
1279 // raise modal dialogs in case a non modal window was selected to activate the app
1281 wxNode
* node
= wxModalDialogs
.GetFirst();
1284 wxDialog
* dialog
= (wxDialog
*) node
->GetData();
1287 node
= node
->GetNext();
1292 void wxApp::MacConvertPrivateToPublicScrap()
1296 void wxApp::MacConvertPublicToPrivateScrap()
1300 void wxApp::MacDoOneEvent()
1305 s_inReceiveEvent
= true ;
1306 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1307 s_inReceiveEvent
= false ;
1308 if ( status
== eventLoopTimedOutErr
)
1310 if ( wxTheApp
->ProcessIdle() )
1311 sleepTime
= kEventDurationNoWait
;
1315 if (g_numberOfThreads
)
1317 sleepTime
= kEventDurationNoWait
;
1320 #endif // wxUSE_THREADS
1322 sleepTime
= kEventDurationSecond
;
1326 else if ( status
== eventLoopQuitErr
)
1328 // according to QA1061 this may also occur when a WakeUp Process
1333 MacHandleOneEvent( theEvent
) ;
1334 ReleaseEvent(theEvent
);
1335 sleepTime
= kEventDurationNoWait
;
1340 EventMask eventMask
= everyEvent
;
1342 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1344 MacHandleModifierEvents( &event
) ;
1345 MacHandleOneEvent( &event
);
1349 MacHandleModifierEvents( &event
) ;
1351 WindowPtr window
= ::FrontWindow() ;
1353 ::IdleControls( window
) ;
1355 if ( wxTheApp
->ProcessIdle() )
1356 sleepTime
= kEventDurationNoWait
;
1360 if (g_numberOfThreads
)
1362 sleepTime
= kEventDurationNoWait
;
1365 #endif // wxUSE_THREADS
1367 sleepTime
= kEventDurationSecond
;
1371 if ( event
.what
!= kHighLevelEvent
)
1372 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1376 DeletePendingObjects() ;
1377 wxMacProcessNotifierAndPendingEvents() ;
1380 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1382 // Override to process unhandled events as you please
1385 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1388 EventTargetRef theTarget
;
1389 theTarget
= GetEventDispatcherTarget();
1390 m_macCurrentEvent
= evr
;
1391 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1392 if(status
== eventNotHandledErr
)
1394 MacHandleUnhandledEvent(evr
);
1397 EventRecord
* ev
= (EventRecord
*) evr
;
1398 m_macCurrentEvent
= ev
;
1400 wxApp::sm_lastMessageTime
= ev
->when
;
1405 MacHandleMouseDownEvent( ev
) ;
1406 if ( ev
->modifiers
& controlKey
)
1407 s_lastMouseDown
= 2;
1409 s_lastMouseDown
= 1;
1412 if ( s_lastMouseDown
== 2 )
1414 ev
->modifiers
|= controlKey
;
1418 ev
->modifiers
&= ~controlKey
;
1420 MacHandleMouseUpEvent( ev
) ;
1421 s_lastMouseDown
= 0;
1424 MacHandleActivateEvent( ev
) ;
1427 // In embedded mode we first let the UnhandledEvent function
1428 // try to handle the update event. If we handle it ourselves
1429 // first and then pass it on, the host's windows won't update.
1430 MacHandleUnhandledEvent(ev
);
1431 MacHandleUpdateEvent( ev
) ;
1435 MacHandleKeyDownEvent( ev
) ;
1438 MacHandleKeyUpEvent( ev
) ;
1441 MacHandleDiskEvent( ev
) ;
1444 MacHandleOSEvent( ev
) ;
1446 case kHighLevelEvent
:
1447 MacHandleHighLevelEvent( ev
) ;
1453 wxMacProcessNotifierAndPendingEvents() ;
1457 bool s_macIsInModalLoop
= false ;
1459 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1461 EventRecord
* ev
= (EventRecord
*) evr
;
1462 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1464 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1466 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1467 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1468 event
.m_altDown
= ev
->modifiers
& optionKey
;
1469 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1471 event
.m_x
= ev
->where
.h
;
1472 event
.m_y
= ev
->where
.v
;
1473 event
.m_timeStamp
= ev
->when
;
1474 wxWindow
* focus
= wxWindow::FindFocus() ;
1475 event
.SetEventObject(focus
);
1477 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1479 event
.m_keyCode
= WXK_CONTROL
;
1480 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1481 focus
->GetEventHandler()->ProcessEvent( event
) ;
1483 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1485 event
.m_keyCode
= WXK_SHIFT
;
1486 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1487 focus
->GetEventHandler()->ProcessEvent( event
) ;
1489 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1491 event
.m_keyCode
= WXK_ALT
;
1492 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1493 focus
->GetEventHandler()->ProcessEvent( event
) ;
1495 if ( ( ev
->modifiers
^ s_lastModifiers
) & cmdKey
)
1497 event
.m_keyCode
= WXK_COMMAND
;
1498 event
.SetEventType( ( ev
->modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1499 focus
->GetEventHandler()->ProcessEvent( event
) ;
1501 s_lastModifiers
= ev
->modifiers
;
1505 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1507 // we must avoid reentrancy problems when processing high level events eg printing
1508 bool former
= s_inYield
;
1510 EventRecord
* ev
= (EventRecord
*) evr
;
1511 ::AEProcessAppleEvent( ev
) ;
1512 s_inYield
= former
;
1515 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1517 EventRecord
* ev
= (EventRecord
*) evr
;
1518 wxToolTip::RemoveToolTips() ;
1521 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1522 WindowAttributes frontWindowAttributes
= NULL
;
1524 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1526 short windowPart
= ::FindWindow(ev
->where
, &window
);
1527 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1528 if ( wxPendingDelete
.Member(win
) )
1532 GetQDGlobalsScreenBits( &screenBits
);
1537 if ( s_macIsInModalLoop
)
1543 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1544 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1545 s_lastMouseDown
= 0;
1549 SystemClick( ev
, window
) ;
1550 s_lastMouseDown
= 0;
1553 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1559 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1564 Point pt
= { 0, 0 } ;
1565 SetPortWindowPort(window
) ;
1566 LocalToGlobal( &pt
) ;
1568 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1569 -1 , wxSIZE_USE_EXISTING
);
1571 s_lastMouseDown
= 0;
1575 if (TrackGoAway(window
, ev
->where
))
1580 s_lastMouseDown
= 0;
1584 Rect newContentRect
;
1585 Rect constraintRect
;
1586 constraintRect
.top
= win
->GetMinHeight() ;
1587 if ( constraintRect
.top
== -1 )
1588 constraintRect
.top
= 0 ;
1589 constraintRect
.left
= win
->GetMinWidth() ;
1590 if ( constraintRect
.left
== -1 )
1591 constraintRect
.left
= 0 ;
1592 constraintRect
.right
= win
->GetMaxWidth() ;
1593 if ( constraintRect
.right
== -1 )
1594 constraintRect
.right
= 32000 ;
1595 constraintRect
.bottom
= win
->GetMaxHeight() ;
1596 if ( constraintRect
.bottom
== -1 )
1597 constraintRect
.bottom
= 32000 ;
1599 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1600 &constraintRect
, &newContentRect
) ;
1603 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1604 newContentRect
.right
- newContentRect
.left
,
1605 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1607 s_lastMouseDown
= 0;
1612 if (TrackBox(window
, ev
->where
, windowPart
))
1614 // TODO setup size event
1615 ZoomWindow( window
, windowPart
, false ) ;
1621 Point pt
= { 0, 0 } ;
1622 SetPortWindowPort(window
) ;
1623 LocalToGlobal( &pt
) ;
1626 GetWindowPortBounds(window
, &tempRect
) ;
1627 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1628 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1631 s_lastMouseDown
= 0;
1633 case inCollapseBox
:
1634 // TODO setup size event
1635 s_lastMouseDown
= 0;
1642 SetPortWindowPort(window
) ;
1645 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1647 if ( s_macIsInModalLoop
)
1651 else if ( UMAIsWindowFloating( window
) )
1654 win
->MacMouseDown( ev
, windowPart
) ;
1658 // Activate window first
1659 ::SelectWindow( window
) ;
1663 win
->MacMouseDown( ev
, windowPart
) ;
1669 win
->MacMouseDown( ev
, windowPart
) ;
1677 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1679 EventRecord
* ev
= (EventRecord
*) evr
;
1682 short windowPart
= inNoWindow
;
1683 if ( wxTheApp
->s_captureWindow
)
1685 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1686 windowPart
= inContent
;
1690 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1701 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1703 win
->MacMouseUp( ev
, windowPart
) ;
1711 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1712 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1717 case kHomeCharCode
:
1720 case kEnterCharCode
:
1721 retval
= WXK_RETURN
;
1726 case kHelpCharCode
:
1729 case kBackspaceCharCode
:
1735 case kPageUpCharCode
:
1736 retval
= WXK_PAGEUP
;
1738 case kPageDownCharCode
:
1739 retval
= WXK_PAGEDOWN
;
1741 case kReturnCharCode
:
1742 retval
= WXK_RETURN
;
1744 case kFunctionKeyCharCode
:
1796 case kEscapeCharCode
:
1797 retval
= WXK_ESCAPE
;
1799 case kLeftArrowCharCode
:
1802 case kRightArrowCharCode
:
1803 retval
= WXK_RIGHT
;
1805 case kUpArrowCharCode
:
1808 case kDownArrowCharCode
:
1811 case kDeleteCharCode
:
1812 retval
= WXK_DELETE
;
1820 int wxKeyCodeToMacModifier(wxKeyCode key
)
1845 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1848 // wxHIDKeyboard keyboard;
1849 // return keyboard.IsActive(key);
1851 // TODO: Have it use HID Manager on OSX...
1852 //if OS X > 10.2 (i.e. 10.2.x)
1853 //a known apple bug prevents the system from determining led
1854 //states with GetKeys... can only determine caps lock led
1855 return !!(GetCurrentKeyModifiers() & wxKeyCodeToMacModifier(key
));
1857 // KeyMapByteArray keymap;
1858 // GetKeys((BigEndianLong*)keymap);
1859 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1864 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1866 EventRecord
* ev
= (EventRecord
*) evr
;
1867 wxToolTip::RemoveToolTips() ;
1869 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1870 if ( HiWord( menuresult
) )
1872 if ( !s_macIsInModalLoop
)
1873 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1877 wxWindow
* focus
= wxWindow::FindFocus() ;
1879 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1882 // we must handle control keys the other way round, otherwise text content is updated too late
1883 // has not been handled -> perform default
1884 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1885 if ( control
&& control
->GetMacControl() != NULL
)
1889 keychar
= short(ev
->message
& charCodeMask
);
1890 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1891 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1898 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1900 EventRecord
* ev
= (EventRecord
*) evr
;
1901 wxToolTip::RemoveToolTips() ;
1903 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1904 if ( HiWord( menuresult
) )
1909 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1915 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1922 keychar
= short(keymessage
& charCodeMask
);
1923 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1925 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1927 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1928 // and look at the character after
1930 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1931 keychar
= short(keyInfo
& charCodeMask
);
1932 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1934 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1935 long realkeyval
= keyval
;
1936 if ( keyval
== keychar
)
1938 // 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)
1939 realkeyval
= short(keymessage
& charCodeMask
) ;
1940 keyval
= wxToupper( keyval
) ;
1943 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1944 bool handled
= false ;
1945 event
.m_shiftDown
= modifiers
& shiftKey
;
1946 event
.m_controlDown
= modifiers
& controlKey
;
1947 event
.m_altDown
= modifiers
& optionKey
;
1948 event
.m_metaDown
= modifiers
& cmdKey
;
1949 event
.m_keyCode
= keyval
;
1953 event
.m_timeStamp
= when
;
1954 event
.SetEventObject(focus
);
1955 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1956 if ( handled
&& event
.GetSkipped() )
1963 wxWindow
*ancestor
= focus
;
1966 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1969 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1970 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1973 if (ancestor
->IsTopLevel())
1975 ancestor
= ancestor
->GetParent();
1978 #endif // wxUSE_ACCEL
1982 event
.Skip( FALSE
) ;
1983 event
.SetEventType( wxEVT_CHAR
) ;
1985 event
.m_keyCode
= realkeyval
;
1987 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1988 if ( handled
&& event
.GetSkipped() )
1992 (keyval
== WXK_TAB
) &&
1993 // CS: copied the change below from wxGTK
1994 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1995 // have this style, yet choose not to process this particular TAB in which
1996 // case TAB must still work as a navigational character
1998 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
2000 (focus
->GetParent()) &&
2001 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
2003 wxNavigationKeyEvent new_event
;
2004 new_event
.SetEventObject( focus
);
2005 new_event
.SetDirection( !event
.ShiftDown() );
2006 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
2007 new_event
.SetWindowChange( event
.ControlDown() );
2008 new_event
.SetCurrentFocus( focus
);
2009 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2010 if ( handled
&& new_event
.GetSkipped() )
2013 // backdoor handler for default return and command escape
2014 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
2016 // if window is not having a focus still testing for default enter or cancel
2017 // TODO add the UMA version for ActiveNonFloatingWindow
2018 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
2021 if ( keyval
== WXK_RETURN
)
2023 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
2025 if ( def
&& def
->IsEnabled() )
2027 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
2028 event
.SetEventObject(def
);
2029 def
->Command(event
);
2033 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
2034 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
2036 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
2037 new_event
.SetEventObject( focus
);
2038 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2045 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
2052 keychar
= short(keymessage
& charCodeMask
);
2053 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
2054 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
2056 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2057 // and look at the character after
2059 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2060 keychar
= short(keyInfo
& charCodeMask
);
2061 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2063 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2065 if ( keyval
== keychar
)
2067 keyval
= wxToupper( keyval
) ;
2069 bool handled
= false ;
2071 wxKeyEvent
event(wxEVT_KEY_UP
);
2072 event
.m_shiftDown
= modifiers
& shiftKey
;
2073 event
.m_controlDown
= modifiers
& controlKey
;
2074 event
.m_altDown
= modifiers
& optionKey
;
2075 event
.m_metaDown
= modifiers
& cmdKey
;
2076 event
.m_keyCode
= keyval
;
2080 event
.m_timeStamp
= when
;
2081 event
.SetEventObject(focus
);
2082 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2088 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
2090 EventRecord
* ev
= (EventRecord
*) evr
;
2091 WindowRef window
= (WindowRef
) ev
->message
;
2094 bool activate
= (ev
->modifiers
& activeFlag
) ;
2095 WindowClass wclass
;
2096 ::GetWindowClass ( window
, &wclass
) ;
2097 if ( wclass
== kFloatingWindowClass
)
2099 // if it is a floater we activate/deactivate the front non-floating window instead
2100 window
= ::FrontNonFloatingWindow() ;
2102 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2104 win
->MacActivate( ev
->when
, activate
) ;
2108 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
2110 EventRecord
* ev
= (EventRecord
*) evr
;
2111 WindowRef window
= (WindowRef
) ev
->message
;
2112 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2115 if ( !wxPendingDelete
.Member(win
) )
2116 win
->MacUpdate( ev
->when
) ;
2120 // since there is no way of telling this foreign window to update itself
2121 // we have to invalidate the update region otherwise we keep getting the same
2122 // event over and over again
2123 BeginUpdate( window
) ;
2124 EndUpdate( window
) ;
2128 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
2130 EventRecord
* ev
= (EventRecord
*) evr
;
2131 if ( HiWord( ev
->message
) != noErr
)
2135 SetPt( &point
, 100 , 100 ) ;
2137 err
= DIBadMount( point
, ev
->message
) ;
2138 wxASSERT( err
== noErr
) ;
2142 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2144 EventRecord
* ev
= (EventRecord
*) evr
;
2145 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2147 case suspendResumeMessage
:
2149 bool isResuming
= ev
->message
& resumeFlag
;
2150 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2152 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2155 WindowRef oldFrontWindow
= NULL
;
2156 WindowRef newFrontWindow
= NULL
;
2158 // in case we don't take care of activating ourselves, we have to synchronize
2159 // our idea of the active window with the process manager's - which it already activated
2161 if ( !doesActivate
)
2162 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2164 MacResume( convertClipboard
) ;
2166 newFrontWindow
= ::FrontNonFloatingWindow() ;
2168 if ( oldFrontWindow
)
2170 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
2172 win
->MacActivate( ev
->when
, false ) ;
2174 if ( newFrontWindow
)
2176 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
2178 win
->MacActivate( ev
->when
, true ) ;
2183 MacSuspend( convertClipboard
) ;
2187 case mouseMovedMessage
:
2191 wxWindow
* currentMouseWindow
= NULL
;
2193 if (s_captureWindow
)
2195 currentMouseWindow
= s_captureWindow
;
2199 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2200 ¤tMouseWindow
) ;
2203 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2205 wxMouseEvent event
;
2207 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2208 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2210 event
.m_leftDown
= isDown
&& !controlDown
;
2211 event
.m_middleDown
= FALSE
;
2212 event
.m_rightDown
= isDown
&& controlDown
;
2213 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2214 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2215 event
.m_altDown
= ev
->modifiers
& optionKey
;
2216 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2217 event
.m_x
= ev
->where
.h
;
2218 event
.m_y
= ev
->where
.v
;
2219 event
.m_timeStamp
= ev
->when
;
2220 event
.SetEventObject(this);
2222 if ( wxWindow::s_lastMouseWindow
)
2224 wxMouseEvent
eventleave(event
);
2225 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2226 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2227 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2229 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2231 if ( currentMouseWindow
)
2233 wxMouseEvent
evententer(event
);
2234 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2235 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2236 evententer
.SetEventObject( currentMouseWindow
) ;
2237 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2239 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2242 short windowPart
= inNoWindow
;
2244 if ( s_captureWindow
)
2246 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2247 windowPart
= inContent
;
2251 windowPart
= ::FindWindow(ev
->where
, &window
);
2258 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2260 win
->MacMouseMoved( ev
, windowPart
) ;
2267 UMAShowArrowCursor();
2277 UMAShowArrowCursor();
2288 void wxApp::MacHandleMouseMovedEvent(wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
2292 wxWindow
* currentMouseWindow
= NULL
;
2294 if (s_captureWindow
)
2296 currentMouseWindow
= s_captureWindow
;
2300 wxWindow::MacGetWindowFromPoint( wxPoint( x
, y
) , ¤tMouseWindow
) ;
2303 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2305 wxMouseEvent event
;
2307 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
2308 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
2310 event
.m_leftDown
= isDown
&& !controlDown
;
2312 event
.m_middleDown
= FALSE
;
2313 event
.m_rightDown
= isDown
&& controlDown
;
2315 event
.m_shiftDown
= modifiers
& shiftKey
;
2316 event
.m_controlDown
= modifiers
& controlKey
;
2317 event
.m_altDown
= modifiers
& optionKey
;
2318 event
.m_metaDown
= modifiers
& cmdKey
;
2322 event
.m_timeStamp
= timestamp
;
2324 if ( wxWindow::s_lastMouseWindow
)
2326 wxMouseEvent
eventleave(event
);
2327 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2328 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2329 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2332 wxToolTip::RelayEvent( wxWindow::s_lastMouseWindow
, eventleave
);
2333 #endif // wxUSE_TOOLTIPS
2334 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2336 if ( currentMouseWindow
)
2338 wxMouseEvent
evententer(event
);
2339 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2340 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2341 evententer
.SetEventObject( currentMouseWindow
) ;
2343 wxToolTip::RelayEvent( currentMouseWindow
, evententer
);
2344 #endif // wxUSE_TOOLTIPS
2345 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2347 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2350 short windowPart
= inNoWindow
;
2352 if ( s_captureWindow
)
2354 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2355 windowPart
= inContent
;
2359 Point pt
= { y
, x
} ;
2360 windowPart
= ::FindWindow(pt
, &window
);
2367 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2369 win
->MacFireMouseEvent( nullEvent
, x
, y
, modifiers
, timestamp
) ;
2376 UMAShowArrowCursor();
2386 UMAShowArrowCursor();
2393 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2395 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2396 wxFrame
* frame
= mbar
->GetFrame();
2397 wxCHECK_RET( mbar
!= NULL
&& frame
!= NULL
, wxT("error in menu item callback") );
2400 frame
->ProcessCommand(id
);
2405 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2408 return; // no menu item selected
2410 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2413 Str255 deskAccessoryName
;
2416 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2417 GetPort(&savedPort
);
2418 OpenDeskAcc(deskAccessoryName
);
2425 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2426 MacHandleMenuCommand( id
) ;