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 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
)
95 const short kMacMinHeap
= (29 * 1024) ;
96 // platform specific static variables
98 const short kwxMacMenuBarResource
= 1 ;
99 const short kwxMacAppleMenuId
= 1 ;
101 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
102 wxWindow
* wxApp::s_captureWindow
= NULL
;
103 int wxApp::s_lastMouseDown
= 0 ;
104 long wxApp::sm_lastMessageTime
= 0;
105 long wxApp::s_lastModifiers
= 0 ;
108 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
109 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
110 long wxApp::s_macPreferencesMenuItemId
= wxID_PREFERENCES
;
111 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
112 wxString
wxApp::s_macHelpMenuTitleName
= wxT("&Help") ;
114 // Normally we're not a plugin
115 bool wxApp::sm_isEmbedded
= false;
116 //----------------------------------------------------------------------
117 // Core Apple Event Support
118 //----------------------------------------------------------------------
120 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
121 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
122 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
123 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
124 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
126 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
128 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
131 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
133 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
136 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
138 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
141 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
143 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
146 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
148 return wxTheApp
->MacHandleAERApp( (AppleEvent
*) event
, reply
) ;
151 // AEODoc Calls MacOpenFile on each of the files passed
153 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
157 DescType returnedType
;
163 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
167 err
= AECountItems(&docList
, &itemsInList
);
171 ProcessSerialNumber PSN
;
172 PSN
.highLongOfPSN
= 0 ;
173 PSN
.lowLongOfPSN
= kCurrentProcess
;
174 SetFrontProcess( &PSN
) ;
176 for (i
= 1; i
<= itemsInList
; i
++) {
177 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
178 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
179 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
185 // AEPDoc Calls MacPrintFile on each of the files passed
187 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
191 DescType returnedType
;
197 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
201 err
= AECountItems(&docList
, &itemsInList
);
205 ProcessSerialNumber PSN
;
206 PSN
.highLongOfPSN
= 0 ;
207 PSN
.lowLongOfPSN
= kCurrentProcess
;
208 SetFrontProcess( &PSN
) ;
210 for (i
= 1; i
<= itemsInList
; i
++) {
211 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
212 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
213 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
219 // AEOApp calls MacNewFile
221 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
227 // AEQuit attempts to quit the application
229 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
231 wxWindow
* win
= GetTopWindow() ;
234 wxCommandEvent
exitEvent(wxEVT_COMMAND_MENU_SELECTED
, s_macExitMenuItemId
);
235 if (!win
->ProcessEvent(exitEvent
))
245 // AEROApp calls MacReopenApp
247 short wxApp::MacHandleAERApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
254 //----------------------------------------------------------------------
255 // Support Routines linking the Mac...File Calls to the Document Manager
256 //----------------------------------------------------------------------
258 void wxApp::MacOpenFile(const wxString
& fileName
)
260 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
262 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
265 void wxApp::MacPrintFile(const wxString
& fileName
)
267 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
270 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
273 wxView
* view
= doc
->GetFirstView() ;
276 wxPrintout
*printout
= view
->OnCreatePrintout();
280 printer
.Print(view
->GetFrame(), printout
, TRUE
);
286 doc
->DeleteAllViews();
287 dm
->RemoveDocument(doc
) ;
293 void wxApp::MacNewFile()
297 void wxApp::MacReopenApp()
299 // eventually check for open docs, if none, call MacNewFile
302 //----------------------------------------------------------------------
303 // Carbon Event Handler
304 //----------------------------------------------------------------------
308 static const EventTypeSpec eventList
[] =
310 { kEventClassCommand
, kEventProcessCommand
} ,
311 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
313 { kEventClassMenu
, kEventMenuOpening
},
314 { kEventClassMenu
, kEventMenuClosed
},
315 { kEventClassMenu
, kEventMenuTargetItem
},
317 { kEventClassApplication
, kEventAppActivated
} ,
318 { kEventClassApplication
, kEventAppDeactivated
} ,
319 // handling the quit event is not recommended by apple
320 // rather using the quit apple event - which we do
322 { kEventClassAppleEvent
, kEventAppleEvent
} ,
324 { kEventClassMouse
, kEventMouseDown
} ,
325 { kEventClassMouse
, kEventMouseMoved
} ,
326 { kEventClassMouse
, kEventMouseUp
} ,
327 { kEventClassMouse
, kEventMouseDragged
} ,
331 static pascal OSStatus
332 MenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
334 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar();
338 wxFrame
* win
= mbar
->GetFrame();
342 // VZ: we could find the menu from its handle here by examining all
343 // the menus in the menu bar recursively but knowing that neither
344 // wxMSW nor wxGTK do it why bother...
348 GetEventParameter(event
,
349 kEventParamDirectObject
,
351 sizeof(menuRef
), NULL
,
357 switch (GetEventKind(event
))
359 case kEventMenuOpening
:
360 type
= wxEVT_MENU_OPEN
;
362 case kEventMenuClosed
:
363 type
= wxEVT_MENU_CLOSE
;
365 case kEventMenuTargetItem
:
366 type
= wxEVT_MENU_HIGHLIGHT
;
367 GetEventParameter(event
, kEventParamMenuCommand
,
368 typeMenuCommand
, NULL
,
369 sizeof(cmd
), NULL
, &cmd
);
370 if (cmd
== 0) return eventNotHandledErr
;
373 wxFAIL_MSG(wxT("Unexpected menu event kind"));
377 wxMenuEvent
wxevent(type
, cmd
);
378 wxevent
.SetEventObject(win
);
380 (void)win
->GetEventHandler()->ProcessEvent(wxevent
);
384 return eventNotHandledErr
;
387 // due to the rather low-level event API of wxWidgets, we cannot use RunApplicationEventLoop
388 // but have to use ReceiveNextEvent dealing with events manually, therefore we also have
389 // to deal with clicks in the menu bar explicitly
391 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
) ;
393 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
395 OSStatus result
= eventNotHandledErr
;
398 UInt32 modifiers
= 0;
399 EventMouseButton button
= 0 ;
402 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
403 sizeof( Point
), NULL
, &point
);
404 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
,
405 sizeof( UInt32
), NULL
, &modifiers
);
406 GetEventParameter( event
, kEventParamMouseButton
, typeMouseButton
, NULL
,
407 sizeof( EventMouseButton
), NULL
, &button
);
408 GetEventParameter( event
, kEventParamClickCount
, typeUInt32
, NULL
,
409 sizeof( UInt32
), NULL
, &click
);
411 if ( button
== 0 || GetEventKind( event
) == kEventMouseUp
)
412 modifiers
+= btnState
;
415 switch( GetEventKind(event
) )
417 case kEventMouseDown
:
421 short windowPart
= ::FindWindow(point
, &window
);
423 if ( windowPart
== inMenuBar
)
425 MenuSelect( point
) ;
430 case kEventMouseDragged
:
433 if ( wxTheApp
->s_captureWindow
)
434 wxMacWindowEventHandler( handler
, event
, (void*) wxTheApp
->s_captureWindow
->MacGetTopLevelWindow() ) ;
437 case kEventMouseMoved
:
439 wxTheApp
->MacHandleMouseMovedEvent( point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
449 static pascal OSStatus
CommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
451 OSStatus result
= eventNotHandledErr
;
455 GetEventParameter( event
, kEventParamDirectObject
, typeHICommand
, NULL
,
456 sizeof( HICommand
), NULL
, &command
);
458 MenuCommand id
= command
.commandID
;
459 if ( id
== kHICommandPreferences
)
460 id
= wxApp::s_macPreferencesMenuItemId
;
462 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
463 wxMenu
* menu
= NULL
;
464 wxMenuItem
* item
= NULL
;
468 item
= mbar
->FindItem( id
, &menu
) ;
469 // it is not 100 % sure that an menu of id 0 is really ours, safety check
470 if ( id
== 0 && menu
!= NULL
&& menu
->GetHMenu() != command
.menu
.menuRef
)
477 if ( item
== NULL
|| menu
== NULL
|| mbar
== NULL
)
480 switch( GetEventKind( event
) )
482 case kEventProcessCommand
:
484 if (item
->IsCheckable())
486 item
->Check( !item
->IsChecked() ) ;
489 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
493 case kEventCommandUpdateStatus
:
494 // eventually trigger an updateui round
504 static pascal OSStatus
ApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
506 OSStatus result
= eventNotHandledErr
;
507 switch ( GetEventKind( event
) )
509 case kEventAppActivated
:
512 wxTheApp
->MacResume( true ) ;
516 case kEventAppDeactivated
:
519 wxTheApp
->MacSuspend( true ) ;
529 pascal OSStatus
wxAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
531 OSStatus result
= eventNotHandledErr
;
532 switch( GetEventClass( event
) )
534 case kEventClassCommand
:
535 result
= CommandEventHandler( handler
, event
, data
) ;
537 case kEventClassApplication
:
538 result
= ApplicationEventHandler( handler
, event
, data
) ;
540 case kEventClassMenu
:
541 result
= MenuEventHandler( handler
, event
, data
) ;
543 case kEventClassMouse
:
544 result
= MouseEventHandler( handler
, event
, data
) ;
546 case kEventClassAppleEvent
:
549 wxMacConvertEventToRecord( event
, &rec
) ;
550 result
= AEProcessAppleEvent( &rec
) ;
560 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler
)
564 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
565 // we know it's there ;-)
566 WXIMPORT
char std::__throws_bad_alloc
;
569 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
575 UMAInitToolbox( 4, sm_isEmbedded
) ;
576 SetEventMask( everyEvent
) ;
577 UMAShowWatchCursor() ;
579 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
580 // open shared library resources from here since we don't have
581 // __wxinitialize in Mach-O shared libraries
582 wxStAppResource::OpenSharedLibraryResource(NULL
);
586 // test the minimal configuration necessary
592 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
594 error
= kMacSTRWrongMachine
;
596 else if (theMachine
< gestaltMacPlus
)
598 error
= kMacSTRWrongMachine
;
600 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
602 error
= kMacSTROldSystem
;
604 else if ( theSystem
< 0x0860 )
606 error
= kMacSTROldSystem
;
608 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
610 error
= kMacSTRSmallSize
;
616 if ( !UMAHasAppearance() )
618 error = kMacSTRNoPre8Yet ;
624 // if we encountered any problems so far, give the error code and exit immediately
628 wxStAppResource resload
;
632 GetIndString(message
, 128, error
);
633 UMAShowArrowCursor() ;
634 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
635 itemHit
= Alert(128, nil
);
640 # if __option(profile)
641 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
646 // now avoid exceptions thrown for new (bad_alloc)
647 // FIXME CS for some changes outside wxMac does not compile anymore
649 std::__throws_bad_alloc
= 0 ;
654 s_macCursorRgn
= ::NewRgn() ;
656 // Mac OS X passes a process serial number command line argument when
657 // the application is launched from the Finder. This argument must be
658 // removed from the command line arguments before being handled by the
659 // application (otherwise applications would need to handle it)
662 static const wxChar
*ARG_PSN
= _T("-psn_");
663 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
665 // remove this argument
667 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
671 if ( !wxAppBase::Initialize(argc
, argv
) )
675 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
679 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
680 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
682 wxMacCreateNotifierTable() ;
684 UMAShowArrowCursor() ;
689 bool wxApp::OnInitGui()
691 if( !wxAppBase::OnInitGui() )
695 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
699 InstallApplicationEventHandler(
700 GetwxAppEventHandlerUPP(),
701 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
707 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
708 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
709 NewAEEventHandlerUPP(AEHandleODoc
) ,
711 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
712 NewAEEventHandlerUPP(AEHandleOApp
) ,
714 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
715 NewAEEventHandlerUPP(AEHandlePDoc
) ,
717 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
718 NewAEEventHandlerUPP(AEHandleRApp
) ,
720 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
721 NewAEEventHandlerUPP(AEHandleQuit
) ,
724 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
725 NewAEEventHandlerProc(AEHandleODoc
) ,
727 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
728 NewAEEventHandlerProc(AEHandleOApp
) ,
730 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
731 NewAEEventHandlerProc(AEHandlePDoc
) ,
733 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
734 NewAEEventHandlerProc(AEHandleRApp
) ,
736 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
737 NewAEEventHandlerProc(AEHandleQuit
) ,
745 void wxApp::CleanUp()
747 wxToolTip::RemoveToolTips() ;
749 // One last chance for pending objects to be cleaned up
750 wxTheApp
->DeletePendingObjects();
752 wxMacDestroyNotifierTable() ;
754 delete wxWinMacWindowList
;
755 wxWinMacWindowList
= NULL
;
757 delete wxWinMacControlList
;
758 wxWinMacControlList
= NULL
;
761 # if __option(profile)
762 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
767 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
768 // close shared library resources from here since we don't have
769 // __wxterminate in Mach-O shared libraries
770 wxStAppResource::CloseSharedLibraryResource();
773 UMACleanupToolbox() ;
774 if (s_macCursorRgn
) {
775 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
782 wxAppBase::CleanUp();
785 //----------------------------------------------------------------------
786 // misc initialization stuff
787 //----------------------------------------------------------------------
789 // extern variable for shared library resource id
790 // need to be able to find it with NSLookupAndBindSymbol
791 short gSharedLibraryResource
= kResFileNotOpened
;
793 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
794 CFBundleRef gSharedLibraryBundle
= NULL
;
795 #endif /* WXMAKINGDLL_CORE && __DARWIN__ */
797 wxStAppResource::wxStAppResource()
799 m_currentRefNum
= CurResFile() ;
800 if ( gSharedLibraryResource
!= kResFileNotOpened
)
802 UseResFile( gSharedLibraryResource
) ;
806 wxStAppResource::~wxStAppResource()
808 if ( m_currentRefNum
!= kResFileNotOpened
)
810 UseResFile( m_currentRefNum
) ;
814 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
816 gSharedLibraryResource
= kResFileNotOpened
;
818 #ifdef WXMAKINGDLL_CORE
819 if ( initBlock
!= NULL
) {
820 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
821 FSSpec
*fileSpec
= NULL
;
823 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
824 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
826 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
827 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
830 if (fileSpec
!= NULL
) {
831 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
836 // Open the shared library resource file if it is not yet open
839 const char *theLibPath
;
841 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWidgets"));
842 if (gSharedLibraryBundle
!= NULL
) {
843 // wxWidgets has been bundled into a framework
844 // load the framework resources
846 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
849 // wxWidgets is a simple dynamic shared library
850 // load the resources from the data fork of a separate resource file
854 OSErr theErr
= noErr
;
856 // get the library path
857 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
858 theModule
= NSModuleForSymbol(theSymbol
);
859 theLibPath
= NSLibraryNameForModule(theModule
);
861 // if we call wxLogDebug from here then, as wxTheApp hasn't been
862 // created yet when we're called from wxApp::Initialize(), wxLog
863 // is going to create a default stderr-based log target instead of
864 // the expected normal GUI one -- don't do it, if we really want
865 // to see this message just use fprintf() here
867 wxLogDebug( wxT("wxMac library installation name is '%s'"),
871 // allocate copy to replace .dylib.* extension with .rsrc
872 if (theLibPath
!= NULL
) {
874 theResPath
= wxString(theLibPath
, wxConvLocal
);
876 theResPath
= wxString(theLibPath
);
878 // replace '_core' with '' in case of multi-lib build
879 theResPath
.Replace(wxT("_core"), wxEmptyString
);
880 // replace ".dylib" shared library extension with ".rsrc"
881 theResPath
.Replace(wxT(".dylib"), wxT(".rsrc"));
882 // Find the begining of the filename
883 theName
= theResPath
.AfterLast('/');
886 wxLogDebug( wxT("wxMac resources file name is '%s'"),
887 theResPath
.mb_str() );
890 theErr
= FSPathMakeRef((UInt8
*) theResPath
.mb_str(), &theResRef
, false);
891 if (theErr
!= noErr
) {
892 // try in current directory (using name only)
893 theErr
= FSPathMakeRef((UInt8
*) theName
.mb_str(), &theResRef
, false);
896 // open the resource file
897 if (theErr
== noErr
) {
898 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
899 &gSharedLibraryResource
);
901 if (theErr
!= noErr
) {
903 wxLogDebug( wxT("unable to open wxMac resource file '%s'\n"),
904 theResPath
.mb_str() );
905 #endif // __WXDEBUG__
910 #endif /* __DARWIN__ */
912 #endif /* WXMAKINGDLL_CORE */
915 void wxStAppResource::CloseSharedLibraryResource()
917 #ifdef WXMAKINGDLL_CORE
918 // Close the shared library resource file
919 if (gSharedLibraryResource
!= kResFileNotOpened
) {
921 if (gSharedLibraryBundle
!= NULL
) {
922 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
923 gSharedLibraryResource
);
924 gSharedLibraryBundle
= NULL
;
927 #endif /* __DARWIN__ */
929 CloseResFile(gSharedLibraryResource
);
931 gSharedLibraryResource
= kResFileNotOpened
;
933 #endif /* WXMAKINGDLL_CORE */
936 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
938 // for shared libraries we have to manually get the correct resource
939 // ref num upon initializing and releasing when terminating, therefore
940 // the __wxinitialize and __wxterminate must be used
943 void __sinit(void); /* (generated by linker) */
944 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
945 pascal void __terminate(void);
948 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
950 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
951 return __initialize( theInitBlock
) ;
954 pascal void __wxterminate(void)
956 wxStAppResource::CloseSharedLibraryResource() ;
960 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
964 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
966 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
967 OSStatus err
= noErr
;
970 switch( GetEventClass( event
) )
972 case kEventClassKeyboard
:
975 switch( GetEventKind(event
) )
977 case kEventRawKeyDown
:
978 rec
->what
= keyDown
;
980 case kEventRawKeyRepeat
:
981 rec
->what
= autoKey
;
983 case kEventRawKeyUp
:
986 case kEventRawKeyModifiersChanged
:
987 rec
->what
= nullEvent
;
996 unsigned char charCode
;
998 GetMouse( &rec
->where
) ;
1000 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1001 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1002 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1003 rec
->modifiers
= modifiers
;
1004 rec
->message
= (keyCode
<< 8 ) + charCode
;
1008 case kEventClassTextInput
:
1010 switch( GetEventKind( event
) )
1012 case kEventTextInputUnicodeForKeyEvent
:
1015 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
1019 unsigned char charCode
;
1021 GetMouse( &rec
->where
) ;
1022 rec
->what
= keyDown
;
1023 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1024 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1025 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1026 rec
->modifiers
= modifiers
;
1027 rec
->message
= (keyCode
<< 8 ) + charCode
;
1043 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1045 OSStatus result = eventNotHandledErr ;
1048 switch ( GetEventClass( event ) )
1050 case kEventClassKeyboard :
1051 if ( wxMacConvertEventToRecord( event , &rec ) )
1053 wxTheApp->MacHandleModifierEvents( &rec ) ;
1054 wxTheApp->MacHandleOneEvent( &rec ) ;
1058 case kEventClassTextInput :
1059 if ( wxMacConvertEventToRecord( event , &rec ) )
1061 wxTheApp->MacHandleModifierEvents( &rec ) ;
1062 wxTheApp->MacHandleOneEvent( &rec ) ;
1076 m_printMode
= wxPRINT_WINDOWS
;
1079 m_macCurrentEvent
= NULL
;
1081 m_macCurrentEventHandlerCallRef
= NULL
;
1085 int wxApp::MainLoop()
1097 void wxApp::ExitMainLoop()
1099 m_keepGoing
= FALSE
;
1102 // Is a message/event pending?
1103 bool wxApp::Pending()
1106 // without the receive event (with pull param = false ) nothing is ever reported
1108 ReceiveNextEvent (0, NULL
, kEventDurationNoWait
, false, &theEvent
);
1109 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1113 return EventAvail( everyEvent
, &event
) ;
1117 // Dispatch a message.
1118 bool wxApp::Dispatch()
1125 void wxApp::OnIdle(wxIdleEvent
& event
)
1127 wxAppBase::OnIdle(event
);
1129 // If they are pending events, we must process them: pending events are
1130 // either events to the threads other than main or events posted with
1131 // wxPostEvent() functions
1132 wxMacProcessNotifierAndPendingEvents();
1134 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1135 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1138 void wxApp::WakeUpIdle()
1149 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1152 GetTopWindow()->Close(TRUE
);
1155 // Default behaviour: close the application with prompts. The
1156 // user can veto the close, and therefore the end session.
1157 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1161 if (!GetTopWindow()->Close(!event
.CanVeto()))
1166 extern "C" void wxCYield() ;
1172 // Yield to other processes
1174 bool wxApp::Yield(bool onlyIfNeeded
)
1178 if ( !onlyIfNeeded
)
1180 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1189 YieldToAnyThread() ;
1191 // by definition yield should handle all non-processed events
1195 OSStatus status
= noErr
;
1198 s_inReceiveEvent
= true ;
1199 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1200 s_inReceiveEvent
= false ;
1202 if ( status
== eventLoopTimedOutErr
)
1204 // make sure next time the event loop will trigger idle events
1205 sleepTime
= kEventDurationNoWait
;
1207 else if ( status
== eventLoopQuitErr
)
1209 // according to QA1061 this may also occur when a WakeUp Process
1214 MacHandleOneEvent( theEvent
) ;
1215 ReleaseEvent(theEvent
);
1217 } while( status
== noErr
) ;
1221 // having a larger value here leads to large performance slowdowns
1222 // so we cannot give background apps more processor time here
1223 // we do so however having a large sleep value in the main event loop
1226 while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1228 MacHandleModifierEvents( &event
) ;
1229 MacHandleOneEvent( &event
);
1230 if ( event
.what
!= kHighLevelEvent
)
1231 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1233 MacHandleModifierEvents( &event
) ;
1236 wxMacProcessNotifierAndPendingEvents() ;
1242 // platform specifics
1244 void wxApp::MacSuspend( bool convertClipboard
)
1247 // we have to deactive the top level windows manually
1249 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1252 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1254 #if 0 // having problems right now with that
1255 if (!win
->HasFlag(wxSTAY_ON_TOP
))
1258 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1260 node
= node
->GetNext();
1263 ::HideFloatingWindows() ;
1265 s_lastMouseDown
= 0 ;
1267 if( convertClipboard
)
1269 MacConvertPrivateToPublicScrap() ;
1273 extern wxList wxModalDialogs
;
1275 void wxApp::MacResume( bool convertClipboard
)
1277 s_lastMouseDown
= 0 ;
1278 if( convertClipboard
)
1280 MacConvertPublicToPrivateScrap() ;
1284 ::ShowFloatingWindows() ;
1285 // raise modal dialogs in case a non modal window was selected to activate the app
1287 wxNode
* node
= wxModalDialogs
.GetFirst();
1290 wxDialog
* dialog
= (wxDialog
*) node
->GetData();
1293 node
= node
->GetNext();
1298 void wxApp::MacConvertPrivateToPublicScrap()
1302 void wxApp::MacConvertPublicToPrivateScrap()
1306 void wxApp::MacDoOneEvent()
1311 s_inReceiveEvent
= true ;
1312 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1313 s_inReceiveEvent
= false ;
1314 if ( status
== eventLoopTimedOutErr
)
1316 if ( wxTheApp
->ProcessIdle() )
1317 sleepTime
= kEventDurationNoWait
;
1321 if (g_numberOfThreads
)
1323 sleepTime
= kEventDurationNoWait
;
1326 #endif // wxUSE_THREADS
1328 sleepTime
= kEventDurationSecond
;
1332 else if ( status
== eventLoopQuitErr
)
1334 // according to QA1061 this may also occur when a WakeUp Process
1339 MacHandleOneEvent( theEvent
) ;
1340 ReleaseEvent(theEvent
);
1341 sleepTime
= kEventDurationNoWait
;
1346 EventMask eventMask
= everyEvent
;
1348 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1350 MacHandleModifierEvents( &event
) ;
1351 MacHandleOneEvent( &event
);
1355 MacHandleModifierEvents( &event
) ;
1357 WindowPtr window
= ::FrontWindow() ;
1359 ::IdleControls( window
) ;
1361 if ( wxTheApp
->ProcessIdle() )
1362 sleepTime
= kEventDurationNoWait
;
1366 if (g_numberOfThreads
)
1368 sleepTime
= kEventDurationNoWait
;
1371 #endif // wxUSE_THREADS
1373 sleepTime
= kEventDurationSecond
;
1377 if ( event
.what
!= kHighLevelEvent
)
1378 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1382 DeletePendingObjects() ;
1383 wxMacProcessNotifierAndPendingEvents() ;
1386 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1388 // Override to process unhandled events as you please
1391 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1394 EventTargetRef theTarget
;
1395 theTarget
= GetEventDispatcherTarget();
1396 m_macCurrentEvent
= evr
;
1397 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1398 if(status
== eventNotHandledErr
)
1400 MacHandleUnhandledEvent(evr
);
1403 EventRecord
* ev
= (EventRecord
*) evr
;
1404 m_macCurrentEvent
= ev
;
1406 wxApp::sm_lastMessageTime
= ev
->when
;
1411 MacHandleMouseDownEvent( ev
) ;
1412 if ( ev
->modifiers
& controlKey
)
1413 s_lastMouseDown
= 2;
1415 s_lastMouseDown
= 1;
1418 if ( s_lastMouseDown
== 2 )
1420 ev
->modifiers
|= controlKey
;
1424 ev
->modifiers
&= ~controlKey
;
1426 MacHandleMouseUpEvent( ev
) ;
1427 s_lastMouseDown
= 0;
1430 MacHandleActivateEvent( ev
) ;
1433 // In embedded mode we first let the UnhandledEvent function
1434 // try to handle the update event. If we handle it ourselves
1435 // first and then pass it on, the host's windows won't update.
1436 MacHandleUnhandledEvent(ev
);
1437 MacHandleUpdateEvent( ev
) ;
1441 MacHandleKeyDownEvent( ev
) ;
1444 MacHandleKeyUpEvent( ev
) ;
1447 MacHandleDiskEvent( ev
) ;
1450 MacHandleOSEvent( ev
) ;
1452 case kHighLevelEvent
:
1453 MacHandleHighLevelEvent( ev
) ;
1459 wxMacProcessNotifierAndPendingEvents() ;
1463 bool s_macIsInModalLoop
= false ;
1465 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1467 EventRecord
* ev
= (EventRecord
*) evr
;
1468 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1470 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1472 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1473 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1474 event
.m_altDown
= ev
->modifiers
& optionKey
;
1475 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1477 event
.m_x
= ev
->where
.h
;
1478 event
.m_y
= ev
->where
.v
;
1479 event
.SetTimestamp( ev
->when
);
1480 wxWindow
* focus
= wxWindow::FindFocus() ;
1481 event
.SetEventObject(focus
);
1483 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1485 event
.m_keyCode
= WXK_CONTROL
;
1486 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1487 focus
->GetEventHandler()->ProcessEvent( event
) ;
1489 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1491 event
.m_keyCode
= WXK_SHIFT
;
1492 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1493 focus
->GetEventHandler()->ProcessEvent( event
) ;
1495 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1497 event
.m_keyCode
= WXK_ALT
;
1498 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1499 focus
->GetEventHandler()->ProcessEvent( event
) ;
1501 if ( ( ev
->modifiers
^ s_lastModifiers
) & cmdKey
)
1503 event
.m_keyCode
= WXK_COMMAND
;
1504 event
.SetEventType( ( ev
->modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1505 focus
->GetEventHandler()->ProcessEvent( event
) ;
1507 s_lastModifiers
= ev
->modifiers
;
1511 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1513 // we must avoid reentrancy problems when processing high level events eg printing
1514 bool former
= s_inYield
;
1516 EventRecord
* ev
= (EventRecord
*) evr
;
1517 ::AEProcessAppleEvent( ev
) ;
1518 s_inYield
= former
;
1521 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1523 EventRecord
* ev
= (EventRecord
*) evr
;
1524 wxToolTip::RemoveToolTips() ;
1527 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1528 WindowAttributes frontWindowAttributes
= NULL
;
1530 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1532 short windowPart
= ::FindWindow(ev
->where
, &window
);
1533 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( (WXWindow
) window
) ;
1534 if ( wxPendingDelete
.Member(win
) )
1538 GetQDGlobalsScreenBits( &screenBits
);
1543 if ( s_macIsInModalLoop
)
1549 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1550 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1551 s_lastMouseDown
= 0;
1555 SystemClick( ev
, window
) ;
1556 s_lastMouseDown
= 0;
1559 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1565 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1570 Point pt
= { 0, 0 } ;
1571 SetPortWindowPort(window
) ;
1572 LocalToGlobal( &pt
) ;
1574 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1575 -1 , wxSIZE_USE_EXISTING
);
1577 s_lastMouseDown
= 0;
1581 if (TrackGoAway(window
, ev
->where
))
1586 s_lastMouseDown
= 0;
1590 Rect newContentRect
;
1591 Rect constraintRect
;
1592 constraintRect
.top
= win
->GetMinHeight() ;
1593 if ( constraintRect
.top
== -1 )
1594 constraintRect
.top
= 0 ;
1595 constraintRect
.left
= win
->GetMinWidth() ;
1596 if ( constraintRect
.left
== -1 )
1597 constraintRect
.left
= 0 ;
1598 constraintRect
.right
= win
->GetMaxWidth() ;
1599 if ( constraintRect
.right
== -1 )
1600 constraintRect
.right
= 32000 ;
1601 constraintRect
.bottom
= win
->GetMaxHeight() ;
1602 if ( constraintRect
.bottom
== -1 )
1603 constraintRect
.bottom
= 32000 ;
1605 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1606 &constraintRect
, &newContentRect
) ;
1609 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1610 newContentRect
.right
- newContentRect
.left
,
1611 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1613 s_lastMouseDown
= 0;
1618 if (TrackBox(window
, ev
->where
, windowPart
))
1620 // TODO setup size event
1621 ZoomWindow( window
, windowPart
, false ) ;
1627 Point pt
= { 0, 0 } ;
1628 SetPortWindowPort(window
) ;
1629 LocalToGlobal( &pt
) ;
1632 GetWindowPortBounds(window
, &tempRect
) ;
1633 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1634 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1637 s_lastMouseDown
= 0;
1639 case inCollapseBox
:
1640 // TODO setup size event
1641 s_lastMouseDown
= 0;
1648 SetPortWindowPort(window
) ;
1651 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1653 if ( s_macIsInModalLoop
)
1657 else if ( UMAIsWindowFloating( window
) )
1660 win
->MacMouseDown( ev
, windowPart
) ;
1664 // Activate window first
1665 ::SelectWindow( window
) ;
1669 win
->MacMouseDown( ev
, windowPart
) ;
1675 win
->MacMouseDown( ev
, windowPart
) ;
1683 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1685 EventRecord
* ev
= (EventRecord
*) evr
;
1688 short windowPart
= inNoWindow
;
1689 if ( wxTheApp
->s_captureWindow
)
1691 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1692 windowPart
= inContent
;
1696 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1707 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( (WXWindow
) window
) ;
1709 win
->MacMouseUp( ev
, windowPart
) ;
1717 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1718 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1723 case kHomeCharCode
:
1726 case kEnterCharCode
:
1727 retval
= WXK_RETURN
;
1732 case kHelpCharCode
:
1735 case kBackspaceCharCode
:
1741 case kPageUpCharCode
:
1742 retval
= WXK_PAGEUP
;
1744 case kPageDownCharCode
:
1745 retval
= WXK_PAGEDOWN
;
1747 case kReturnCharCode
:
1748 retval
= WXK_RETURN
;
1750 case kFunctionKeyCharCode
:
1802 case kEscapeCharCode
:
1803 retval
= WXK_ESCAPE
;
1805 case kLeftArrowCharCode
:
1808 case kRightArrowCharCode
:
1809 retval
= WXK_RIGHT
;
1811 case kUpArrowCharCode
:
1814 case kDownArrowCharCode
:
1817 case kDeleteCharCode
:
1818 retval
= WXK_DELETE
;
1826 int wxKeyCodeToMacModifier(wxKeyCode key
)
1851 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1853 wxASSERT_MSG(key
!= WXK_LBUTTON
&& key
!= WXK_RBUTTON
&& key
!=
1854 WXK_MBUTTON
, wxT("can't use wxGetKeyState() for mouse buttons"));
1858 return !!(BitTst(keymap
, (sizeof(KeyMap
)*8) - key
));
1862 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1864 EventRecord
* ev
= (EventRecord
*) evr
;
1865 wxToolTip::RemoveToolTips() ;
1867 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1868 if ( HiWord( menuresult
) )
1870 if ( !s_macIsInModalLoop
)
1871 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1875 wxWindow
* focus
= wxWindow::FindFocus() ;
1877 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1880 // we must handle control keys the other way round, otherwise text content is updated too late
1881 // has not been handled -> perform default
1882 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1883 if ( control
&& control
->GetMacControl() != NULL
)
1887 keychar
= short(ev
->message
& charCodeMask
);
1888 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1889 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1896 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1898 EventRecord
* ev
= (EventRecord
*) evr
;
1899 wxToolTip::RemoveToolTips() ;
1901 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1902 if ( HiWord( menuresult
) )
1907 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1913 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1920 keychar
= short(keymessage
& charCodeMask
);
1921 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1923 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1925 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1926 // and look at the character after
1928 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1929 keychar
= short(keyInfo
& charCodeMask
);
1930 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1932 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1933 long realkeyval
= keyval
;
1934 if ( keyval
== keychar
)
1936 // 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)
1937 realkeyval
= short(keymessage
& charCodeMask
) ;
1938 keyval
= wxToupper( keyval
) ;
1941 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1942 bool handled
= false ;
1943 event
.m_shiftDown
= modifiers
& shiftKey
;
1944 event
.m_controlDown
= modifiers
& controlKey
;
1945 event
.m_altDown
= modifiers
& optionKey
;
1946 event
.m_metaDown
= modifiers
& cmdKey
;
1947 event
.m_keyCode
= keyval
;
1951 event
.SetTimestamp(when
);
1952 event
.SetEventObject(focus
);
1953 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1954 if ( handled
&& event
.GetSkipped() )
1961 wxWindow
*ancestor
= focus
;
1964 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1967 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1968 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1971 if (ancestor
->IsTopLevel())
1973 ancestor
= ancestor
->GetParent();
1976 #endif // wxUSE_ACCEL
1980 event
.Skip( FALSE
) ;
1981 event
.SetEventType( wxEVT_CHAR
) ;
1983 event
.m_keyCode
= realkeyval
;
1985 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1986 if ( handled
&& event
.GetSkipped() )
1990 (keyval
== WXK_TAB
) &&
1991 // CS: copied the change below from wxGTK
1992 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1993 // have this style, yet choose not to process this particular TAB in which
1994 // case TAB must still work as a navigational character
1996 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
1998 (focus
->GetParent()) &&
1999 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
2001 wxNavigationKeyEvent new_event
;
2002 new_event
.SetEventObject( focus
);
2003 new_event
.SetDirection( !event
.ShiftDown() );
2004 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
2005 new_event
.SetWindowChange( event
.ControlDown() );
2006 new_event
.SetCurrentFocus( focus
);
2007 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2008 if ( handled
&& new_event
.GetSkipped() )
2011 // backdoor handler for default return and command escape
2012 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
2014 // if window is not having a focus still testing for default enter or cancel
2015 // TODO add the UMA version for ActiveNonFloatingWindow
2016 wxWindow
* focus
= wxFindWinFromMacWindow( (WXWindow
) FrontWindow() ) ;
2019 if ( keyval
== WXK_RETURN
)
2021 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
2023 if ( def
&& def
->IsEnabled() )
2025 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
2026 event
.SetEventObject(def
);
2027 def
->Command(event
);
2031 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
2032 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
2034 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
2035 new_event
.SetEventObject( focus
);
2036 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2043 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
2050 keychar
= short(keymessage
& charCodeMask
);
2051 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
2052 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
2054 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2055 // and look at the character after
2057 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2058 keychar
= short(keyInfo
& charCodeMask
);
2059 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2061 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2063 if ( keyval
== keychar
)
2065 keyval
= wxToupper( keyval
) ;
2067 bool handled
= false ;
2069 wxKeyEvent
event(wxEVT_KEY_UP
);
2070 event
.m_shiftDown
= modifiers
& shiftKey
;
2071 event
.m_controlDown
= modifiers
& controlKey
;
2072 event
.m_altDown
= modifiers
& optionKey
;
2073 event
.m_metaDown
= modifiers
& cmdKey
;
2074 event
.m_keyCode
= keyval
;
2078 event
.SetTimestamp(when
);
2079 event
.SetEventObject(focus
);
2080 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2086 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
2088 EventRecord
* ev
= (EventRecord
*) evr
;
2089 WindowRef window
= (WindowRef
) ev
->message
;
2092 bool activate
= (ev
->modifiers
& activeFlag
) ;
2093 WindowClass wclass
;
2094 ::GetWindowClass ( window
, &wclass
) ;
2095 if ( wclass
== kFloatingWindowClass
)
2097 // if it is a floater we activate/deactivate the front non-floating window instead
2098 window
= ::FrontNonFloatingWindow() ;
2100 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( (WXWindow
) window
) ;
2102 win
->MacActivate( ev
->when
, activate
) ;
2106 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
2108 EventRecord
* ev
= (EventRecord
*) evr
;
2109 WindowRef window
= (WindowRef
) ev
->message
;
2110 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( (WXWindow
) window
) ;
2113 if ( !wxPendingDelete
.Member(win
) )
2114 win
->MacUpdate( ev
->when
) ;
2118 // since there is no way of telling this foreign window to update itself
2119 // we have to invalidate the update region otherwise we keep getting the same
2120 // event over and over again
2121 BeginUpdate( window
) ;
2122 EndUpdate( window
) ;
2126 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
2128 EventRecord
* ev
= (EventRecord
*) evr
;
2129 if ( HiWord( ev
->message
) != noErr
)
2133 SetPt( &point
, 100 , 100 ) ;
2135 err
= DIBadMount( point
, ev
->message
) ;
2136 wxASSERT( err
== noErr
) ;
2140 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2142 EventRecord
* ev
= (EventRecord
*) evr
;
2143 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2145 case suspendResumeMessage
:
2147 bool isResuming
= ev
->message
& resumeFlag
;
2148 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2150 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2153 WindowRef oldFrontWindow
= NULL
;
2154 WindowRef newFrontWindow
= NULL
;
2156 // in case we don't take care of activating ourselves, we have to synchronize
2157 // our idea of the active window with the process manager's - which it already activated
2159 if ( !doesActivate
)
2160 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2162 MacResume( convertClipboard
) ;
2164 newFrontWindow
= ::FrontNonFloatingWindow() ;
2166 if ( oldFrontWindow
)
2168 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( (WXWindow
) oldFrontWindow
) ;
2170 win
->MacActivate( ev
->when
, false ) ;
2172 if ( newFrontWindow
)
2174 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( (WXWindow
) newFrontWindow
) ;
2176 win
->MacActivate( ev
->when
, true ) ;
2181 MacSuspend( convertClipboard
) ;
2185 case mouseMovedMessage
:
2189 wxWindow
* currentMouseWindow
= NULL
;
2191 if (s_captureWindow
)
2193 currentMouseWindow
= s_captureWindow
;
2197 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2198 ¤tMouseWindow
) ;
2201 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2203 wxMouseEvent event
;
2205 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2206 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2208 event
.m_leftDown
= isDown
&& !controlDown
;
2209 event
.m_middleDown
= FALSE
;
2210 event
.m_rightDown
= isDown
&& controlDown
;
2211 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2212 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2213 event
.m_altDown
= ev
->modifiers
& optionKey
;
2214 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2215 event
.m_x
= ev
->where
.h
;
2216 event
.m_y
= ev
->where
.v
;
2217 event
.SetTimestamp( ev
->when
);
2218 event
.SetEventObject(this);
2220 if ( wxWindow::s_lastMouseWindow
)
2222 wxMouseEvent
eventleave(event
);
2223 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2224 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2225 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2227 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2229 if ( currentMouseWindow
)
2231 wxMouseEvent
evententer(event
);
2232 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2233 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2234 evententer
.SetEventObject( currentMouseWindow
) ;
2235 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2237 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2240 short windowPart
= inNoWindow
;
2242 if ( s_captureWindow
)
2244 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2245 windowPart
= inContent
;
2249 windowPart
= ::FindWindow(ev
->where
, &window
);
2256 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( (WXWindow
) window
) ;
2258 win
->MacMouseMoved( ev
, windowPart
) ;
2265 UMAShowArrowCursor();
2275 UMAShowArrowCursor();
2286 void wxApp::MacHandleMouseMovedEvent(wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
2290 wxWindow
* currentMouseWindow
= NULL
;
2292 if (s_captureWindow
)
2294 currentMouseWindow
= s_captureWindow
;
2298 wxWindow::MacGetWindowFromPoint( wxPoint( x
, y
) , ¤tMouseWindow
) ;
2301 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2303 wxMouseEvent event
;
2305 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
2306 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
2308 event
.m_leftDown
= isDown
&& !controlDown
;
2310 event
.m_middleDown
= FALSE
;
2311 event
.m_rightDown
= isDown
&& controlDown
;
2313 event
.m_shiftDown
= modifiers
& shiftKey
;
2314 event
.m_controlDown
= modifiers
& controlKey
;
2315 event
.m_altDown
= modifiers
& optionKey
;
2316 event
.m_metaDown
= modifiers
& cmdKey
;
2320 event
.SetTimestamp(timestamp
);
2322 if ( wxWindow::s_lastMouseWindow
)
2324 wxMouseEvent
eventleave(event
);
2325 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2326 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2327 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2330 wxToolTip::RelayEvent( wxWindow::s_lastMouseWindow
, eventleave
);
2331 #endif // wxUSE_TOOLTIPS
2332 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2334 if ( currentMouseWindow
)
2336 wxMouseEvent
evententer(event
);
2337 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2338 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2339 evententer
.SetEventObject( currentMouseWindow
) ;
2341 wxToolTip::RelayEvent( currentMouseWindow
, evententer
);
2342 #endif // wxUSE_TOOLTIPS
2343 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2345 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2348 short windowPart
= inNoWindow
;
2350 if ( s_captureWindow
)
2352 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2353 windowPart
= inContent
;
2357 Point pt
= { y
, x
} ;
2358 windowPart
= ::FindWindow(pt
, &window
);
2365 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2367 win
->MacFireMouseEvent( nullEvent
, x
, y
, modifiers
, timestamp
) ;
2374 UMAShowArrowCursor();
2384 UMAShowArrowCursor();
2391 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2393 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2394 wxFrame
* frame
= mbar
->GetFrame();
2395 wxCHECK_RET( mbar
!= NULL
&& frame
!= NULL
, wxT("error in menu item callback") );
2398 frame
->ProcessCommand(id
);
2403 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2406 return; // no menu item selected
2408 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2411 Str255 deskAccessoryName
;
2414 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2415 GetPort(&savedPort
);
2416 OpenDeskAcc(deskAccessoryName
);
2423 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2424 MacHandleMenuCommand( id
) ;