1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/app.cpp
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
24 #include "wx/window.h"
27 #include "wx/button.h"
31 #include "wx/palette.h"
33 #include "wx/cursor.h"
34 #include "wx/dialog.h"
35 #include "wx/msgdlg.h"
36 #include "wx/textctrl.h"
37 #include "wx/memory.h"
38 #include "wx/gdicmn.h"
41 #include "wx/module.h"
42 #include "wx/tooltip.h"
43 #include "wx/docview.h"
44 #include "wx/filename.h"
58 #include "wx/mac/uma.h"
59 #include "wx/mac/macnotfy.h"
62 # include <CoreServices/CoreServices.h>
63 # if defined(WXMAKINGDLL_CORE)
64 # include <mach-o/dyld.h>
69 # include <ToolUtils.h>
70 # include <DiskInit.h>
74 extern wxList wxPendingDelete
;
75 extern wxList
*wxWinMacWindowList
;
76 extern wxList
*wxWinMacControlList
;
78 extern size_t g_numberOfThreads
;
79 #endif // wxUSE_THREADS
81 // statics for implementation
83 static bool s_inYield
= false;
86 static bool s_inReceiveEvent
= false ;
87 static EventTime sleepTime
= kEventDurationNoWait
;
89 static long sleepTime
= 0 ;
92 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
93 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
94 EVT_IDLE(wxApp::OnIdle
)
95 EVT_END_SESSION(wxApp::OnEndSession
)
96 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
99 const short kMacMinHeap
= (29 * 1024) ;
100 // platform specific static variables
102 const short kwxMacMenuBarResource
= 1 ;
103 const short kwxMacAppleMenuId
= 1 ;
105 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
106 wxWindow
* wxApp::s_captureWindow
= NULL
;
107 int wxApp::s_lastMouseDown
= 0 ;
108 long wxApp::sm_lastMessageTime
= 0;
109 long wxApp::s_lastModifiers
= 0 ;
112 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
113 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
114 long wxApp::s_macPreferencesMenuItemId
= wxID_PREFERENCES
;
115 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
116 wxString
wxApp::s_macHelpMenuTitleName
= wxT("&Help") ;
118 // Normally we're not a plugin
119 bool wxApp::sm_isEmbedded
= false;
120 //----------------------------------------------------------------------
121 // Core Apple Event Support
122 //----------------------------------------------------------------------
124 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
125 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
126 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
127 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
128 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
130 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
132 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
135 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
137 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
140 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
142 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
145 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
147 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
150 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
152 return wxTheApp
->MacHandleAERApp( (AppleEvent
*) event
, reply
) ;
155 // AEODoc Calls MacOpenFile on each of the files passed
157 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
161 DescType returnedType
;
167 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
171 err
= AECountItems(&docList
, &itemsInList
);
175 ProcessSerialNumber PSN
;
176 PSN
.highLongOfPSN
= 0 ;
177 PSN
.lowLongOfPSN
= kCurrentProcess
;
178 SetFrontProcess( &PSN
) ;
180 for (i
= 1; i
<= itemsInList
; i
++) {
181 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
182 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
183 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
189 // AEPDoc Calls MacPrintFile on each of the files passed
191 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
195 DescType returnedType
;
201 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
205 err
= AECountItems(&docList
, &itemsInList
);
209 ProcessSerialNumber PSN
;
210 PSN
.highLongOfPSN
= 0 ;
211 PSN
.lowLongOfPSN
= kCurrentProcess
;
212 SetFrontProcess( &PSN
) ;
214 for (i
= 1; i
<= itemsInList
; i
++) {
215 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
216 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
217 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
223 // AEOApp calls MacNewFile
225 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
231 // AEQuit attempts to quit the application
233 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
235 wxWindow
* win
= GetTopWindow() ;
238 wxCommandEvent
exitEvent(wxEVT_COMMAND_MENU_SELECTED
, s_macExitMenuItemId
);
239 if (!win
->ProcessEvent(exitEvent
))
249 // AEROApp calls MacReopenApp
251 short wxApp::MacHandleAERApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
258 //----------------------------------------------------------------------
259 // Support Routines linking the Mac...File Calls to the Document Manager
260 //----------------------------------------------------------------------
262 void wxApp::MacOpenFile(const wxString
& fileName
)
264 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
266 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
269 void wxApp::MacPrintFile(const wxString
& fileName
)
271 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
274 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
277 wxView
* view
= doc
->GetFirstView() ;
280 wxPrintout
*printout
= view
->OnCreatePrintout();
284 printer
.Print(view
->GetFrame(), printout
, true);
290 doc
->DeleteAllViews();
291 dm
->RemoveDocument(doc
) ;
297 void wxApp::MacNewFile()
301 void wxApp::MacReopenApp()
303 // eventually check for open docs, if none, call MacNewFile
306 //----------------------------------------------------------------------
307 // Carbon Event Handler
308 //----------------------------------------------------------------------
312 static const EventTypeSpec eventList
[] =
314 { kEventClassCommand
, kEventProcessCommand
} ,
315 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
317 { kEventClassMenu
, kEventMenuOpening
},
318 { kEventClassMenu
, kEventMenuClosed
},
319 { kEventClassMenu
, kEventMenuTargetItem
},
321 { kEventClassApplication
, kEventAppActivated
} ,
322 { kEventClassApplication
, kEventAppDeactivated
} ,
323 // handling the quit event is not recommended by apple
324 // rather using the quit apple event - which we do
326 { kEventClassAppleEvent
, kEventAppleEvent
} ,
328 { kEventClassMouse
, kEventMouseDown
} ,
329 { kEventClassMouse
, kEventMouseMoved
} ,
330 { kEventClassMouse
, kEventMouseUp
} ,
331 { kEventClassMouse
, kEventMouseDragged
} ,
335 static pascal OSStatus
336 MenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
338 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar();
342 wxFrame
* win
= mbar
->GetFrame();
346 // VZ: we could find the menu from its handle here by examining all
347 // the menus in the menu bar recursively but knowing that neither
348 // wxMSW nor wxGTK do it why bother...
352 GetEventParameter(event
,
353 kEventParamDirectObject
,
355 sizeof(menuRef
), NULL
,
361 switch (GetEventKind(event
))
363 case kEventMenuOpening
:
364 type
= wxEVT_MENU_OPEN
;
366 case kEventMenuClosed
:
367 type
= wxEVT_MENU_CLOSE
;
369 case kEventMenuTargetItem
:
370 type
= wxEVT_MENU_HIGHLIGHT
;
371 GetEventParameter(event
, kEventParamMenuCommand
,
372 typeMenuCommand
, NULL
,
373 sizeof(cmd
), NULL
, &cmd
);
374 if (cmd
== 0) return eventNotHandledErr
;
377 wxFAIL_MSG(wxT("Unexpected menu event kind"));
381 wxMenuEvent
wxevent(type
, cmd
);
382 wxevent
.SetEventObject(win
);
384 (void)win
->GetEventHandler()->ProcessEvent(wxevent
);
388 return eventNotHandledErr
;
391 // due to the rather low-level event API of wxWidgets, we cannot use RunApplicationEventLoop
392 // but have to use ReceiveNextEvent dealing with events manually, therefore we also have
393 // to deal with clicks in the menu bar explicitly
395 pascal OSStatus
wxMacWindowEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
) ;
397 static pascal OSStatus
MouseEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
399 OSStatus result
= eventNotHandledErr
;
402 UInt32 modifiers
= 0;
403 EventMouseButton button
= 0 ;
406 GetEventParameter( event
, kEventParamMouseLocation
, typeQDPoint
, NULL
,
407 sizeof( Point
), NULL
, &point
);
408 GetEventParameter( event
, kEventParamKeyModifiers
, typeUInt32
, NULL
,
409 sizeof( UInt32
), NULL
, &modifiers
);
410 GetEventParameter( event
, kEventParamMouseButton
, typeMouseButton
, NULL
,
411 sizeof( EventMouseButton
), NULL
, &button
);
412 GetEventParameter( event
, kEventParamClickCount
, typeUInt32
, NULL
,
413 sizeof( UInt32
), NULL
, &click
);
415 if ( button
== 0 || GetEventKind( event
) == kEventMouseUp
)
416 modifiers
+= btnState
;
419 switch( GetEventKind(event
) )
421 case kEventMouseDown
:
425 short windowPart
= ::FindWindow(point
, &window
);
427 if ( windowPart
== inMenuBar
)
429 MenuSelect( point
) ;
434 case kEventMouseDragged
:
437 if ( wxTheApp
->s_captureWindow
)
438 wxMacWindowEventHandler( handler
, event
, (void*) wxTheApp
->s_captureWindow
->MacGetTopLevelWindow() ) ;
441 case kEventMouseMoved
:
443 wxTheApp
->MacHandleMouseMovedEvent( point
.h
, point
.v
, modifiers
, EventTimeToTicks( GetEventTime( event
) ) ) ;
453 static pascal OSStatus
CommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
455 OSStatus result
= eventNotHandledErr
;
459 GetEventParameter( event
, kEventParamDirectObject
, typeHICommand
, NULL
,
460 sizeof( HICommand
), NULL
, &command
);
462 MenuCommand id
= command
.commandID
;
463 if ( id
== kHICommandPreferences
)
464 id
= wxApp::s_macPreferencesMenuItemId
;
466 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
467 wxMenu
* menu
= NULL
;
468 wxMenuItem
* item
= NULL
;
472 item
= mbar
->FindItem( id
, &menu
) ;
473 // it is not 100 % sure that an menu of id 0 is really ours, safety check
474 if ( id
== 0 && menu
!= NULL
&& menu
->GetHMenu() != command
.menu
.menuRef
)
481 if ( item
== NULL
|| menu
== NULL
|| mbar
== NULL
)
484 switch( GetEventKind( event
) )
486 case kEventProcessCommand
:
488 if (item
->IsCheckable())
490 item
->Check( !item
->IsChecked() ) ;
493 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
497 case kEventCommandUpdateStatus
:
498 // eventually trigger an updateui round
508 static pascal OSStatus
ApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
510 OSStatus result
= eventNotHandledErr
;
511 switch ( GetEventKind( event
) )
513 case kEventAppActivated
:
516 wxTheApp
->MacResume( true ) ;
520 case kEventAppDeactivated
:
523 wxTheApp
->MacSuspend( true ) ;
533 pascal OSStatus
wxAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
535 OSStatus result
= eventNotHandledErr
;
536 switch( GetEventClass( event
) )
538 case kEventClassCommand
:
539 result
= CommandEventHandler( handler
, event
, data
) ;
541 case kEventClassApplication
:
542 result
= ApplicationEventHandler( handler
, event
, data
) ;
544 case kEventClassMenu
:
545 result
= MenuEventHandler( handler
, event
, data
) ;
547 case kEventClassMouse
:
548 result
= MouseEventHandler( handler
, event
, data
) ;
550 case kEventClassAppleEvent
:
553 wxMacConvertEventToRecord( event
, &rec
) ;
554 result
= AEProcessAppleEvent( &rec
) ;
564 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler
)
568 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
569 // we know it's there ;-)
570 WXIMPORT
char std::__throws_bad_alloc
;
573 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
579 UMAInitToolbox( 4, sm_isEmbedded
) ;
580 SetEventMask( everyEvent
) ;
581 UMAShowWatchCursor() ;
583 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
584 // open shared library resources from here since we don't have
585 // __wxinitialize in Mach-O shared libraries
586 wxStAppResource::OpenSharedLibraryResource(NULL
);
590 // test the minimal configuration necessary
596 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
598 error
= kMacSTRWrongMachine
;
600 else if (theMachine
< gestaltMacPlus
)
602 error
= kMacSTRWrongMachine
;
604 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
606 error
= kMacSTROldSystem
;
608 else if ( theSystem
< 0x0860 )
610 error
= kMacSTROldSystem
;
612 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
614 error
= kMacSTRSmallSize
;
620 if ( !UMAHasAppearance() )
622 error = kMacSTRNoPre8Yet ;
628 // if we encountered any problems so far, give the error code and exit immediately
632 wxStAppResource resload
;
636 GetIndString(message
, 128, error
);
637 UMAShowArrowCursor() ;
638 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
639 itemHit
= Alert(128, nil
);
644 # if __option(profile)
645 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
650 // now avoid exceptions thrown for new (bad_alloc)
651 // FIXME CS for some changes outside wxMac does not compile anymore
653 std::__throws_bad_alloc
= 0 ;
658 s_macCursorRgn
= ::NewRgn() ;
660 // Mac OS X passes a process serial number command line argument when
661 // the application is launched from the Finder. This argument must be
662 // removed from the command line arguments before being handled by the
663 // application (otherwise applications would need to handle it)
666 static const wxChar
*ARG_PSN
= _T("-psn_");
667 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
669 // remove this argument
671 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
675 if ( !wxAppBase::Initialize(argc
, argv
) )
679 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
683 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
684 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
686 wxMacCreateNotifierTable() ;
688 UMAShowArrowCursor() ;
693 bool wxApp::OnInitGui()
695 if( !wxAppBase::OnInitGui() )
699 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
703 InstallApplicationEventHandler(
704 GetwxAppEventHandlerUPP(),
705 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
711 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
712 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
713 NewAEEventHandlerUPP(AEHandleODoc
) ,
715 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
716 NewAEEventHandlerUPP(AEHandleOApp
) ,
718 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
719 NewAEEventHandlerUPP(AEHandlePDoc
) ,
721 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
722 NewAEEventHandlerUPP(AEHandleRApp
) ,
724 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
725 NewAEEventHandlerUPP(AEHandleQuit
) ,
728 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
729 NewAEEventHandlerProc(AEHandleODoc
) ,
731 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
732 NewAEEventHandlerProc(AEHandleOApp
) ,
734 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
735 NewAEEventHandlerProc(AEHandlePDoc
) ,
737 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
738 NewAEEventHandlerProc(AEHandleRApp
) ,
740 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
741 NewAEEventHandlerProc(AEHandleQuit
) ,
749 void wxApp::CleanUp()
751 wxToolTip::RemoveToolTips() ;
753 // One last chance for pending objects to be cleaned up
754 wxTheApp
->DeletePendingObjects();
756 wxMacDestroyNotifierTable() ;
758 delete wxWinMacWindowList
;
759 wxWinMacWindowList
= NULL
;
761 delete wxWinMacControlList
;
762 wxWinMacControlList
= NULL
;
765 # if __option(profile)
766 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
771 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
772 // close shared library resources from here since we don't have
773 // __wxterminate in Mach-O shared libraries
774 wxStAppResource::CloseSharedLibraryResource();
777 UMACleanupToolbox() ;
778 if (s_macCursorRgn
) {
779 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
786 wxAppBase::CleanUp();
789 //----------------------------------------------------------------------
790 // misc initialization stuff
791 //----------------------------------------------------------------------
793 // extern variable for shared library resource id
794 // need to be able to find it with NSLookupAndBindSymbol
795 short gSharedLibraryResource
= kResFileNotOpened
;
797 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
798 CFBundleRef gSharedLibraryBundle
= NULL
;
799 #endif /* WXMAKINGDLL_CORE && __DARWIN__ */
801 wxStAppResource::wxStAppResource()
803 m_currentRefNum
= CurResFile() ;
804 if ( gSharedLibraryResource
!= kResFileNotOpened
)
806 UseResFile( gSharedLibraryResource
) ;
810 wxStAppResource::~wxStAppResource()
812 if ( m_currentRefNum
!= kResFileNotOpened
)
814 UseResFile( m_currentRefNum
) ;
818 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
820 gSharedLibraryResource
= kResFileNotOpened
;
822 #ifdef WXMAKINGDLL_CORE
823 if ( initBlock
!= NULL
) {
824 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
825 FSSpec
*fileSpec
= NULL
;
827 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
828 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
830 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
831 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
834 if (fileSpec
!= NULL
) {
835 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
840 // Open the shared library resource file if it is not yet open
843 const char *theLibPath
;
845 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWidgets"));
846 if (gSharedLibraryBundle
!= NULL
) {
847 // wxWidgets has been bundled into a framework
848 // load the framework resources
850 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
853 // wxWidgets is a simple dynamic shared library
854 // load the resources from the data fork of a separate resource file
858 OSErr theErr
= noErr
;
860 // get the library path
861 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
862 theModule
= NSModuleForSymbol(theSymbol
);
863 theLibPath
= NSLibraryNameForModule(theModule
);
865 // if we call wxLogDebug from here then, as wxTheApp hasn't been
866 // created yet when we're called from wxApp::Initialize(), wxLog
867 // is going to create a default stderr-based log target instead of
868 // the expected normal GUI one -- don't do it, if we really want
869 // to see this message just use fprintf() here
871 wxLogDebug( wxT("wxMac library installation name is '%s'"),
875 // allocate copy to replace .dylib.* extension with .rsrc
876 if (theLibPath
!= NULL
) {
878 theResPath
= wxString(theLibPath
, wxConvLocal
);
880 theResPath
= wxString(theLibPath
);
882 // replace '_core' with '' in case of multi-lib build
883 theResPath
.Replace(wxT("_core"), wxEmptyString
);
884 // replace ".dylib" shared library extension with ".rsrc"
885 theResPath
.Replace(wxT(".dylib"), wxT(".rsrc"));
886 // Find the begining of the filename
887 theName
= theResPath
.AfterLast('/');
890 wxLogDebug( wxT("wxMac resources file name is '%s'"),
891 theResPath
.mb_str() );
894 theErr
= FSPathMakeRef((UInt8
*) theResPath
.mb_str(), &theResRef
, false);
895 if (theErr
!= noErr
) {
896 // try in current directory (using name only)
897 theErr
= FSPathMakeRef((UInt8
*) theName
.mb_str(), &theResRef
, false);
900 // open the resource file
901 if (theErr
== noErr
) {
902 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
903 &gSharedLibraryResource
);
905 if (theErr
!= noErr
) {
907 wxLogDebug( wxT("unable to open wxMac resource file '%s'\n"),
908 theResPath
.mb_str() );
909 #endif // __WXDEBUG__
914 #endif /* __DARWIN__ */
916 #endif /* WXMAKINGDLL_CORE */
919 void wxStAppResource::CloseSharedLibraryResource()
921 #ifdef WXMAKINGDLL_CORE
922 // Close the shared library resource file
923 if (gSharedLibraryResource
!= kResFileNotOpened
) {
925 if (gSharedLibraryBundle
!= NULL
) {
926 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
927 gSharedLibraryResource
);
928 gSharedLibraryBundle
= NULL
;
931 #endif /* __DARWIN__ */
933 CloseResFile(gSharedLibraryResource
);
935 gSharedLibraryResource
= kResFileNotOpened
;
937 #endif /* WXMAKINGDLL_CORE */
940 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
942 // for shared libraries we have to manually get the correct resource
943 // ref num upon initializing and releasing when terminating, therefore
944 // the __wxinitialize and __wxterminate must be used
947 void __sinit(void); /* (generated by linker) */
948 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
949 pascal void __terminate(void);
952 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
954 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
955 return __initialize( theInitBlock
) ;
958 pascal void __wxterminate(void)
960 wxStAppResource::CloseSharedLibraryResource() ;
964 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
968 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
970 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
971 OSStatus err
= noErr
;
974 switch( GetEventClass( event
) )
976 case kEventClassKeyboard
:
979 switch( GetEventKind(event
) )
981 case kEventRawKeyDown
:
982 rec
->what
= keyDown
;
984 case kEventRawKeyRepeat
:
985 rec
->what
= autoKey
;
987 case kEventRawKeyUp
:
990 case kEventRawKeyModifiersChanged
:
991 rec
->what
= nullEvent
;
1000 unsigned char charCode
;
1002 GetMouse( &rec
->where
) ;
1004 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1005 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1006 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1007 rec
->modifiers
= modifiers
;
1008 rec
->message
= (keyCode
<< 8 ) + charCode
;
1012 case kEventClassTextInput
:
1014 switch( GetEventKind( event
) )
1016 case kEventTextInputUnicodeForKeyEvent
:
1019 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
1023 unsigned char charCode
;
1025 GetMouse( &rec
->where
) ;
1026 rec
->what
= keyDown
;
1027 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1028 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1029 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1030 rec
->modifiers
= modifiers
;
1031 rec
->message
= (keyCode
<< 8 ) + charCode
;
1047 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1049 OSStatus result = eventNotHandledErr ;
1052 switch ( GetEventClass( event ) )
1054 case kEventClassKeyboard :
1055 if ( wxMacConvertEventToRecord( event , &rec ) )
1057 wxTheApp->MacHandleModifierEvents( &rec ) ;
1058 wxTheApp->MacHandleOneEvent( &rec ) ;
1062 case kEventClassTextInput :
1063 if ( wxMacConvertEventToRecord( event , &rec ) )
1065 wxTheApp->MacHandleModifierEvents( &rec ) ;
1066 wxTheApp->MacHandleOneEvent( &rec ) ;
1080 m_printMode
= wxPRINT_WINDOWS
;
1083 m_macCurrentEvent
= NULL
;
1085 m_macCurrentEventHandlerCallRef
= NULL
;
1089 int wxApp::MainLoop()
1101 void wxApp::ExitMainLoop()
1103 m_keepGoing
= false;
1106 // Is a message/event pending?
1107 bool wxApp::Pending()
1110 // without the receive event (with pull param = false ) nothing is ever reported
1112 ReceiveNextEvent (0, NULL
, kEventDurationNoWait
, false, &theEvent
);
1113 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1117 return EventAvail( everyEvent
, &event
) ;
1121 // Dispatch a message.
1122 bool wxApp::Dispatch()
1129 void wxApp::OnIdle(wxIdleEvent
& event
)
1131 wxAppBase::OnIdle(event
);
1133 // If they are pending events, we must process them: pending events are
1134 // either events to the threads other than main or events posted with
1135 // wxPostEvent() functions
1136 wxMacProcessNotifierAndPendingEvents();
1138 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1139 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1142 void wxApp::WakeUpIdle()
1153 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1156 GetTopWindow()->Close(true);
1159 // Default behaviour: close the application with prompts. The
1160 // user can veto the close, and therefore the end session.
1161 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1165 if (!GetTopWindow()->Close(!event
.CanVeto()))
1170 extern "C" void wxCYield() ;
1176 // Yield to other processes
1178 bool wxApp::Yield(bool onlyIfNeeded
)
1182 if ( !onlyIfNeeded
)
1184 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1193 YieldToAnyThread() ;
1195 // by definition yield should handle all non-processed events
1199 OSStatus status
= noErr
;
1202 s_inReceiveEvent
= true ;
1203 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1204 s_inReceiveEvent
= false ;
1206 if ( status
== eventLoopTimedOutErr
)
1208 // make sure next time the event loop will trigger idle events
1209 sleepTime
= kEventDurationNoWait
;
1211 else if ( status
== eventLoopQuitErr
)
1213 // according to QA1061 this may also occur when a WakeUp Process
1218 MacHandleOneEvent( theEvent
) ;
1219 ReleaseEvent(theEvent
);
1221 } while( status
== noErr
) ;
1225 // having a larger value here leads to large performance slowdowns
1226 // so we cannot give background apps more processor time here
1227 // we do so however having a large sleep value in the main event loop
1230 while ( !IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1232 MacHandleModifierEvents( &event
) ;
1233 MacHandleOneEvent( &event
);
1234 if ( event
.what
!= kHighLevelEvent
)
1235 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1237 MacHandleModifierEvents( &event
) ;
1240 wxMacProcessNotifierAndPendingEvents() ;
1246 // platform specifics
1248 void wxApp::MacSuspend( bool convertClipboard
)
1251 // we have to deactive the top level windows manually
1253 wxWindowListNode
* node
= wxTopLevelWindows
.GetFirst();
1256 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1258 #if 0 // having problems right now with that
1259 if (!win
->HasFlag(wxSTAY_ON_TOP
))
1262 win
->MacActivate( ((EventRecord
*) MacGetCurrentEvent())->when
, false ) ;
1264 node
= node
->GetNext();
1267 ::HideFloatingWindows() ;
1269 s_lastMouseDown
= 0 ;
1271 if( convertClipboard
)
1273 MacConvertPrivateToPublicScrap() ;
1277 extern wxList wxModalDialogs
;
1279 void wxApp::MacResume( bool convertClipboard
)
1281 s_lastMouseDown
= 0 ;
1282 if( convertClipboard
)
1284 MacConvertPublicToPrivateScrap() ;
1288 ::ShowFloatingWindows() ;
1289 // raise modal dialogs in case a non modal window was selected to activate the app
1291 wxNode
* node
= wxModalDialogs
.GetFirst();
1294 wxDialog
* dialog
= (wxDialog
*) node
->GetData();
1297 node
= node
->GetNext();
1302 void wxApp::MacConvertPrivateToPublicScrap()
1306 void wxApp::MacConvertPublicToPrivateScrap()
1310 void wxApp::MacDoOneEvent()
1315 s_inReceiveEvent
= true ;
1316 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1317 s_inReceiveEvent
= false ;
1318 if ( status
== eventLoopTimedOutErr
)
1320 if ( wxTheApp
->ProcessIdle() )
1321 sleepTime
= kEventDurationNoWait
;
1325 if (g_numberOfThreads
)
1327 sleepTime
= kEventDurationNoWait
;
1330 #endif // wxUSE_THREADS
1332 sleepTime
= kEventDurationSecond
;
1336 else if ( status
== eventLoopQuitErr
)
1338 // according to QA1061 this may also occur when a WakeUp Process
1343 MacHandleOneEvent( theEvent
) ;
1344 ReleaseEvent(theEvent
);
1345 sleepTime
= kEventDurationNoWait
;
1350 EventMask eventMask
= everyEvent
;
1352 if (WaitNextEvent(eventMask
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1354 MacHandleModifierEvents( &event
) ;
1355 MacHandleOneEvent( &event
);
1359 MacHandleModifierEvents( &event
) ;
1361 WindowPtr window
= ::FrontWindow() ;
1363 ::IdleControls( window
) ;
1365 if ( wxTheApp
->ProcessIdle() )
1366 sleepTime
= kEventDurationNoWait
;
1370 if (g_numberOfThreads
)
1372 sleepTime
= kEventDurationNoWait
;
1375 #endif // wxUSE_THREADS
1377 sleepTime
= kEventDurationSecond
;
1381 if ( event
.what
!= kHighLevelEvent
)
1382 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1386 DeletePendingObjects() ;
1387 wxMacProcessNotifierAndPendingEvents() ;
1390 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1392 // Override to process unhandled events as you please
1395 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1398 EventTargetRef theTarget
;
1399 theTarget
= GetEventDispatcherTarget();
1400 m_macCurrentEvent
= evr
;
1401 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1402 if(status
== eventNotHandledErr
)
1404 MacHandleUnhandledEvent(evr
);
1407 EventRecord
* ev
= (EventRecord
*) evr
;
1408 m_macCurrentEvent
= ev
;
1410 wxApp::sm_lastMessageTime
= ev
->when
;
1415 MacHandleMouseDownEvent( ev
) ;
1416 if ( ev
->modifiers
& controlKey
)
1417 s_lastMouseDown
= 2;
1419 s_lastMouseDown
= 1;
1422 if ( s_lastMouseDown
== 2 )
1424 ev
->modifiers
|= controlKey
;
1428 ev
->modifiers
&= ~controlKey
;
1430 MacHandleMouseUpEvent( ev
) ;
1431 s_lastMouseDown
= 0;
1434 MacHandleActivateEvent( ev
) ;
1437 // In embedded mode we first let the UnhandledEvent function
1438 // try to handle the update event. If we handle it ourselves
1439 // first and then pass it on, the host's windows won't update.
1440 MacHandleUnhandledEvent(ev
);
1441 MacHandleUpdateEvent( ev
) ;
1445 MacHandleKeyDownEvent( ev
) ;
1448 MacHandleKeyUpEvent( ev
) ;
1451 MacHandleDiskEvent( ev
) ;
1454 MacHandleOSEvent( ev
) ;
1456 case kHighLevelEvent
:
1457 MacHandleHighLevelEvent( ev
) ;
1463 wxMacProcessNotifierAndPendingEvents() ;
1467 bool s_macIsInModalLoop
= false ;
1469 void wxApp::MacHandleModifierEvents( WXEVENTREF evr
)
1471 EventRecord
* ev
= (EventRecord
*) evr
;
1472 if ( ev
->modifiers
!= s_lastModifiers
&& wxWindow::FindFocus() != NULL
)
1474 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1476 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1477 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1478 event
.m_altDown
= ev
->modifiers
& optionKey
;
1479 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1481 event
.m_x
= ev
->where
.h
;
1482 event
.m_y
= ev
->where
.v
;
1483 event
.SetTimestamp( ev
->when
);
1484 wxWindow
* focus
= wxWindow::FindFocus() ;
1485 event
.SetEventObject(focus
);
1487 if ( (ev
->modifiers
^ s_lastModifiers
) & controlKey
)
1489 event
.m_keyCode
= WXK_CONTROL
;
1490 event
.SetEventType( ( ev
->modifiers
& controlKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1491 focus
->GetEventHandler()->ProcessEvent( event
) ;
1493 if ( (ev
->modifiers
^ s_lastModifiers
) & shiftKey
)
1495 event
.m_keyCode
= WXK_SHIFT
;
1496 event
.SetEventType( ( ev
->modifiers
& shiftKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1497 focus
->GetEventHandler()->ProcessEvent( event
) ;
1499 if ( (ev
->modifiers
^ s_lastModifiers
) & optionKey
)
1501 event
.m_keyCode
= WXK_ALT
;
1502 event
.SetEventType( ( ev
->modifiers
& optionKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1503 focus
->GetEventHandler()->ProcessEvent( event
) ;
1505 if ( ( ev
->modifiers
^ s_lastModifiers
) & cmdKey
)
1507 event
.m_keyCode
= WXK_COMMAND
;
1508 event
.SetEventType( ( ev
->modifiers
& cmdKey
) ? wxEVT_KEY_DOWN
: wxEVT_KEY_UP
) ;
1509 focus
->GetEventHandler()->ProcessEvent( event
) ;
1511 s_lastModifiers
= ev
->modifiers
;
1515 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1517 // we must avoid reentrancy problems when processing high level events eg printing
1518 bool former
= s_inYield
;
1520 EventRecord
* ev
= (EventRecord
*) evr
;
1521 ::AEProcessAppleEvent( ev
) ;
1522 s_inYield
= former
;
1525 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1527 EventRecord
* ev
= (EventRecord
*) evr
;
1528 wxToolTip::RemoveToolTips() ;
1531 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1532 WindowAttributes frontWindowAttributes
= NULL
;
1534 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1536 short windowPart
= ::FindWindow(ev
->where
, &window
);
1537 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( (WXWindow
) window
) ;
1538 if ( wxPendingDelete
.Member(win
) )
1542 GetQDGlobalsScreenBits( &screenBits
);
1547 if ( s_macIsInModalLoop
)
1553 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1554 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1555 s_lastMouseDown
= 0;
1559 SystemClick( ev
, window
) ;
1560 s_lastMouseDown
= 0;
1563 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1569 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1574 Point pt
= { 0, 0 } ;
1575 SetPortWindowPort(window
) ;
1576 LocalToGlobal( &pt
) ;
1578 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1579 -1 , wxSIZE_USE_EXISTING
);
1581 s_lastMouseDown
= 0;
1585 if (TrackGoAway(window
, ev
->where
))
1590 s_lastMouseDown
= 0;
1594 Rect newContentRect
;
1595 Rect constraintRect
;
1596 constraintRect
.top
= win
->GetMinHeight() ;
1597 if ( constraintRect
.top
== -1 )
1598 constraintRect
.top
= 0 ;
1599 constraintRect
.left
= win
->GetMinWidth() ;
1600 if ( constraintRect
.left
== -1 )
1601 constraintRect
.left
= 0 ;
1602 constraintRect
.right
= win
->GetMaxWidth() ;
1603 if ( constraintRect
.right
== -1 )
1604 constraintRect
.right
= 32000 ;
1605 constraintRect
.bottom
= win
->GetMaxHeight() ;
1606 if ( constraintRect
.bottom
== -1 )
1607 constraintRect
.bottom
= 32000 ;
1609 Boolean growResult
= ResizeWindow( window
, ev
->where
,
1610 &constraintRect
, &newContentRect
) ;
1613 win
->SetSize( newContentRect
.left
, newContentRect
.top
,
1614 newContentRect
.right
- newContentRect
.left
,
1615 newContentRect
.bottom
- newContentRect
.top
, wxSIZE_USE_EXISTING
);
1617 s_lastMouseDown
= 0;
1622 if (TrackBox(window
, ev
->where
, windowPart
))
1624 // TODO setup size event
1625 ZoomWindow( window
, windowPart
, false ) ;
1631 Point pt
= { 0, 0 } ;
1632 SetPortWindowPort(window
) ;
1633 LocalToGlobal( &pt
) ;
1636 GetWindowPortBounds(window
, &tempRect
) ;
1637 win
->SetSize( pt
.h
, pt
.v
, tempRect
.right
-tempRect
.left
,
1638 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1641 s_lastMouseDown
= 0;
1643 case inCollapseBox
:
1644 // TODO setup size event
1645 s_lastMouseDown
= 0;
1652 SetPortWindowPort(window
) ;
1655 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1657 if ( s_macIsInModalLoop
)
1661 else if ( UMAIsWindowFloating( window
) )
1664 win
->MacMouseDown( ev
, windowPart
) ;
1668 // Activate window first
1669 ::SelectWindow( window
) ;
1673 win
->MacMouseDown( ev
, windowPart
) ;
1679 win
->MacMouseDown( ev
, windowPart
) ;
1687 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1689 EventRecord
* ev
= (EventRecord
*) evr
;
1692 short windowPart
= inNoWindow
;
1693 if ( wxTheApp
->s_captureWindow
)
1695 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1696 windowPart
= inContent
;
1700 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1711 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( (WXWindow
) window
) ;
1713 win
->MacMouseUp( ev
, windowPart
) ;
1721 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1722 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1727 case kHomeCharCode
:
1730 case kEnterCharCode
:
1731 retval
= WXK_RETURN
;
1736 case kHelpCharCode
:
1739 case kBackspaceCharCode
:
1745 case kPageUpCharCode
:
1746 retval
= WXK_PAGEUP
;
1748 case kPageDownCharCode
:
1749 retval
= WXK_PAGEDOWN
;
1751 case kReturnCharCode
:
1752 retval
= WXK_RETURN
;
1754 case kFunctionKeyCharCode
:
1806 case kEscapeCharCode
:
1807 retval
= WXK_ESCAPE
;
1809 case kLeftArrowCharCode
:
1812 case kRightArrowCharCode
:
1813 retval
= WXK_RIGHT
;
1815 case kUpArrowCharCode
:
1818 case kDownArrowCharCode
:
1821 case kDeleteCharCode
:
1822 retval
= WXK_DELETE
;
1830 int wxKeyCodeToMacModifier(wxKeyCode key
)
1855 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1857 wxASSERT_MSG(key
!= WXK_LBUTTON
&& key
!= WXK_RBUTTON
&& key
!=
1858 WXK_MBUTTON
, wxT("can't use wxGetKeyState() for mouse buttons"));
1862 return !!(BitTst(keymap
, (sizeof(KeyMap
)*8) - key
));
1866 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1868 EventRecord
* ev
= (EventRecord
*) evr
;
1869 wxToolTip::RemoveToolTips() ;
1871 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1872 if ( HiWord( menuresult
) )
1874 if ( !s_macIsInModalLoop
)
1875 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1879 wxWindow
* focus
= wxWindow::FindFocus() ;
1881 if ( MacSendKeyDownEvent( focus
, ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) == false )
1884 // we must handle control keys the other way round, otherwise text content is updated too late
1885 // has not been handled -> perform default
1886 wxControl
* control
= wxDynamicCast( focus
, wxControl
) ;
1887 if ( control
&& control
->GetMacControl() != NULL
)
1891 keychar
= short(ev
->message
& charCodeMask
);
1892 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1893 ::HandleControlKey( (ControlHandle
) control
->GetMacControl() , keycode
, keychar
, ev
->modifiers
) ;
1900 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1902 EventRecord
* ev
= (EventRecord
*) evr
;
1903 wxToolTip::RemoveToolTips() ;
1905 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1906 if ( HiWord( menuresult
) )
1911 MacSendKeyUpEvent( wxWindow::FindFocus() , ev
->message
, ev
->modifiers
, ev
->when
, ev
->where
.h
, ev
->where
.v
) ;
1917 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1924 keychar
= short(keymessage
& charCodeMask
);
1925 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1927 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1929 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1930 // and look at the character after
1932 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1933 keychar
= short(keyInfo
& charCodeMask
);
1934 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1936 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1937 long realkeyval
= keyval
;
1938 if ( keyval
== keychar
)
1940 // 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)
1941 realkeyval
= short(keymessage
& charCodeMask
) ;
1942 keyval
= wxToupper( keyval
) ;
1945 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1946 bool handled
= false ;
1947 event
.m_shiftDown
= modifiers
& shiftKey
;
1948 event
.m_controlDown
= modifiers
& controlKey
;
1949 event
.m_altDown
= modifiers
& optionKey
;
1950 event
.m_metaDown
= modifiers
& cmdKey
;
1951 event
.m_keyCode
= keyval
;
1955 event
.SetTimestamp(when
);
1956 event
.SetEventObject(focus
);
1957 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1958 if ( handled
&& event
.GetSkipped() )
1965 wxWindow
*ancestor
= focus
;
1968 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1971 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1972 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1975 if (ancestor
->IsTopLevel())
1977 ancestor
= ancestor
->GetParent();
1980 #endif // wxUSE_ACCEL
1984 event
.Skip( false ) ;
1985 event
.SetEventType( wxEVT_CHAR
) ;
1987 event
.m_keyCode
= realkeyval
;
1989 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1990 if ( handled
&& event
.GetSkipped() )
1994 (keyval
== WXK_TAB
) &&
1995 // CS: copied the change below from wxGTK
1996 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1997 // have this style, yet choose not to process this particular TAB in which
1998 // case TAB must still work as a navigational character
2000 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
2002 (focus
->GetParent()) &&
2003 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
2005 wxNavigationKeyEvent new_event
;
2006 new_event
.SetEventObject( focus
);
2007 new_event
.SetDirection( !event
.ShiftDown() );
2008 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
2009 new_event
.SetWindowChange( event
.ControlDown() );
2010 new_event
.SetCurrentFocus( focus
);
2011 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2012 if ( handled
&& new_event
.GetSkipped() )
2015 // backdoor handler for default return and command escape
2016 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
2018 // if window is not having a focus still testing for default enter or cancel
2019 // TODO add the UMA version for ActiveNonFloatingWindow
2020 wxWindow
* focus
= wxFindWinFromMacWindow( (WXWindow
) FrontWindow() ) ;
2023 if ( keyval
== WXK_RETURN
)
2025 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
2026 if ( tlw
&& tlw
->GetDefaultItem() )
2028 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
2029 if ( def
&& def
->IsEnabled() )
2031 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
2032 event
.SetEventObject(def
);
2033 def
->Command(event
);
2038 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
2039 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
2041 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
2042 new_event
.SetEventObject( focus
);
2043 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
2050 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
2057 keychar
= short(keymessage
& charCodeMask
);
2058 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
2059 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
2061 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2062 // and look at the character after
2064 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
2065 keychar
= short(keyInfo
& charCodeMask
);
2066 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
2068 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
2070 if ( keyval
== keychar
)
2072 keyval
= wxToupper( keyval
) ;
2074 bool handled
= false ;
2076 wxKeyEvent
event(wxEVT_KEY_UP
);
2077 event
.m_shiftDown
= modifiers
& shiftKey
;
2078 event
.m_controlDown
= modifiers
& controlKey
;
2079 event
.m_altDown
= modifiers
& optionKey
;
2080 event
.m_metaDown
= modifiers
& cmdKey
;
2081 event
.m_keyCode
= keyval
;
2085 event
.SetTimestamp(when
);
2086 event
.SetEventObject(focus
);
2087 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
2093 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
2095 EventRecord
* ev
= (EventRecord
*) evr
;
2096 WindowRef window
= (WindowRef
) ev
->message
;
2099 bool activate
= (ev
->modifiers
& activeFlag
) ;
2100 WindowClass wclass
;
2101 ::GetWindowClass ( window
, &wclass
) ;
2102 if ( wclass
== kFloatingWindowClass
)
2104 // if it is a floater we activate/deactivate the front non-floating window instead
2105 window
= ::FrontNonFloatingWindow() ;
2107 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( (WXWindow
) window
) ;
2109 win
->MacActivate( ev
->when
, activate
) ;
2113 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
2115 EventRecord
* ev
= (EventRecord
*) evr
;
2116 WindowRef window
= (WindowRef
) ev
->message
;
2117 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( (WXWindow
) window
) ;
2120 if ( !wxPendingDelete
.Member(win
) )
2121 win
->MacUpdate( ev
->when
) ;
2125 // since there is no way of telling this foreign window to update itself
2126 // we have to invalidate the update region otherwise we keep getting the same
2127 // event over and over again
2128 BeginUpdate( window
) ;
2129 EndUpdate( window
) ;
2133 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
2135 EventRecord
* ev
= (EventRecord
*) evr
;
2136 if ( HiWord( ev
->message
) != noErr
)
2140 SetPt( &point
, 100 , 100 ) ;
2142 err
= DIBadMount( point
, ev
->message
) ;
2143 wxASSERT( err
== noErr
) ;
2147 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
2149 EventRecord
* ev
= (EventRecord
*) evr
;
2150 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
2152 case suspendResumeMessage
:
2154 bool isResuming
= ev
->message
& resumeFlag
;
2155 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
2157 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
2160 WindowRef oldFrontWindow
= NULL
;
2161 WindowRef newFrontWindow
= NULL
;
2163 // in case we don't take care of activating ourselves, we have to synchronize
2164 // our idea of the active window with the process manager's - which it already activated
2166 if ( !doesActivate
)
2167 oldFrontWindow
= ::FrontNonFloatingWindow() ;
2169 MacResume( convertClipboard
) ;
2171 newFrontWindow
= ::FrontNonFloatingWindow() ;
2173 if ( oldFrontWindow
)
2175 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( (WXWindow
) oldFrontWindow
) ;
2177 win
->MacActivate( ev
->when
, false ) ;
2179 if ( newFrontWindow
)
2181 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( (WXWindow
) newFrontWindow
) ;
2183 win
->MacActivate( ev
->when
, true ) ;
2188 MacSuspend( convertClipboard
) ;
2192 case mouseMovedMessage
:
2196 wxWindow
* currentMouseWindow
= NULL
;
2198 if (s_captureWindow
)
2200 currentMouseWindow
= s_captureWindow
;
2204 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
2205 ¤tMouseWindow
) ;
2208 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2210 wxMouseEvent event
;
2212 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
2213 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
2215 event
.m_leftDown
= isDown
&& !controlDown
;
2216 event
.m_middleDown
= false;
2217 event
.m_rightDown
= isDown
&& controlDown
;
2218 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
2219 event
.m_controlDown
= ev
->modifiers
& controlKey
;
2220 event
.m_altDown
= ev
->modifiers
& optionKey
;
2221 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
2222 event
.m_x
= ev
->where
.h
;
2223 event
.m_y
= ev
->where
.v
;
2224 event
.SetTimestamp( ev
->when
);
2225 event
.SetEventObject(this);
2227 if ( wxWindow::s_lastMouseWindow
)
2229 wxMouseEvent
eventleave(event
);
2230 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2231 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2232 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2234 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2236 if ( currentMouseWindow
)
2238 wxMouseEvent
evententer(event
);
2239 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2240 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2241 evententer
.SetEventObject( currentMouseWindow
) ;
2242 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2244 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2247 short windowPart
= inNoWindow
;
2249 if ( s_captureWindow
)
2251 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2252 windowPart
= inContent
;
2256 windowPart
= ::FindWindow(ev
->where
, &window
);
2263 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( (WXWindow
) window
) ;
2265 win
->MacMouseMoved( ev
, windowPart
) ;
2272 UMAShowArrowCursor();
2282 UMAShowArrowCursor();
2293 void wxApp::MacHandleMouseMovedEvent(wxInt32 x
, wxInt32 y
,wxUint32 modifiers
, long timestamp
)
2297 wxWindow
* currentMouseWindow
= NULL
;
2299 if (s_captureWindow
)
2301 currentMouseWindow
= s_captureWindow
;
2305 wxWindow::MacGetWindowFromPoint( wxPoint( x
, y
) , ¤tMouseWindow
) ;
2308 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
2310 wxMouseEvent event
;
2312 bool isDown
= !(modifiers
& btnState
) ; // 1 is for up
2313 bool controlDown
= modifiers
& controlKey
; // for simulating right mouse
2315 event
.m_leftDown
= isDown
&& !controlDown
;
2317 event
.m_middleDown
= false;
2318 event
.m_rightDown
= isDown
&& controlDown
;
2320 event
.m_shiftDown
= modifiers
& shiftKey
;
2321 event
.m_controlDown
= modifiers
& controlKey
;
2322 event
.m_altDown
= modifiers
& optionKey
;
2323 event
.m_metaDown
= modifiers
& cmdKey
;
2327 event
.SetTimestamp(timestamp
);
2329 if ( wxWindow::s_lastMouseWindow
)
2331 wxMouseEvent
eventleave(event
);
2332 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
2333 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
2334 eventleave
.SetEventObject( wxWindow::s_lastMouseWindow
) ;
2337 wxToolTip::RelayEvent( wxWindow::s_lastMouseWindow
, eventleave
);
2338 #endif // wxUSE_TOOLTIPS
2339 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
2341 if ( currentMouseWindow
)
2343 wxMouseEvent
evententer(event
);
2344 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
2345 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
2346 evententer
.SetEventObject( currentMouseWindow
) ;
2348 wxToolTip::RelayEvent( currentMouseWindow
, evententer
);
2349 #endif // wxUSE_TOOLTIPS
2350 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
2352 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
2355 short windowPart
= inNoWindow
;
2357 if ( s_captureWindow
)
2359 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
2360 windowPart
= inContent
;
2364 Point pt
= { y
, x
} ;
2365 windowPart
= ::FindWindow(pt
, &window
);
2372 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
2374 win
->MacFireMouseEvent( nullEvent
, x
, y
, modifiers
, timestamp
) ;
2381 UMAShowArrowCursor();
2391 UMAShowArrowCursor();
2398 void wxApp::MacHandleMenuCommand( wxUint32 id
)
2400 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
2401 wxFrame
* frame
= mbar
->GetFrame();
2402 wxCHECK_RET( mbar
!= NULL
&& frame
!= NULL
, wxT("error in menu item callback") );
2405 frame
->ProcessCommand(id
);
2410 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
2413 return; // no menu item selected
2415 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
2418 Str255 deskAccessoryName
;
2421 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
2422 GetPort(&savedPort
);
2423 OpenDeskAcc(deskAccessoryName
);
2430 GetMenuItemCommandID( GetMenuHandle(macMenuId
) , macMenuItemNum
, &id
) ;
2431 MacHandleMenuCommand( id
) ;