1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/window.h"
16 #include "wx/button.h"
19 #include "wx/gdicmn.h"
22 #include "wx/cursor.h"
25 #include "wx/palette.h"
27 #include "wx/dialog.h"
28 #include "wx/msgdlg.h"
30 #include "wx/module.h"
31 #include "wx/memory.h"
32 #include "wx/tooltip.h"
33 #include "wx/textctrl.h"
35 #include "wx/docview.h"
36 #include "wx/filename.h"
48 // #include "apprsrc.h"
50 #include "wx/mac/uma.h"
51 #include "wx/mac/macnotfy.h"
54 # include <CoreServices/CoreServices.h>
55 # if defined(WXMAKINGDLL_CORE)
56 # include <mach-o/dyld.h>
61 # include <ToolUtils.h>
62 # include <DiskInit.h>
66 extern wxList wxPendingDelete
;
68 // set wxMAC_USE_RAEL to 1 if RunApplicationEventLoop should be used
69 // if 0 the lower level CarbonEventLoop will be used
70 // on the long run RAEL should replace the low level event loop
71 // we will have to clean up event handling to make sure we don't
72 // miss handling of things like pending events etc
73 // perhaps we will also have to pipe events through an ueber-event-handler
74 // to make sure we have one place to do all these house-keeping functions
76 #define wxMAC_USE_RAEL 0
79 extern size_t g_numberOfThreads
;
80 #endif // wxUSE_THREADS
82 // statics for implementation
84 static bool s_inYield
= false;
86 static bool s_inReceiveEvent
= false ;
87 static EventTime sleepTime
= kEventDurationNoWait
;
89 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
90 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
91 EVT_IDLE(wxApp::OnIdle
)
92 EVT_END_SESSION(wxApp::OnEndSession
)
93 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
97 const short kMacMinHeap
= (29 * 1024) ;
98 // platform specific static variables
100 const short kwxMacMenuBarResource
= 1 ;
101 const short kwxMacAppleMenuId
= 1 ;
103 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
104 wxWindow
* wxApp::s_captureWindow
= NULL
;
105 int wxApp::s_lastMouseDown
= 0 ;
106 long wxApp::sm_lastMessageTime
= 0;
107 long wxApp::s_lastModifiers
= 0 ;
110 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
111 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
112 long wxApp::s_macPreferencesMenuItemId
= wxID_PREFERENCES
;
113 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
114 wxString
wxApp::s_macHelpMenuTitleName
= wxT("&Help") ;
116 // Normally we're not a plugin
117 bool wxApp::sm_isEmbedded
= false;
118 //----------------------------------------------------------------------
119 // Core Apple Event Support
120 //----------------------------------------------------------------------
122 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
123 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
124 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
125 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
126 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
128 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
130 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
133 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
135 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
138 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
140 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
143 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
145 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
148 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
150 return wxTheApp
->MacHandleAERApp( (AppleEvent
*) event
, reply
) ;
153 // AEODoc Calls MacOpenFile on each of the files passed
155 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
159 DescType returnedType
;
164 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
168 err
= AECountItems(&docList
, &itemsInList
);
172 ProcessSerialNumber PSN
;
173 PSN
.highLongOfPSN
= 0 ;
174 PSN
.lowLongOfPSN
= kCurrentProcess
;
175 SetFrontProcess( &PSN
) ;
177 for (i
= 1; i
<= itemsInList
; i
++)
182 AEGetNthPtr(&docList
, i
, typeFSRef
, &keywd
, &returnedType
,
183 (Ptr
) & theRef
, sizeof(theRef
), &actualSize
);
184 fName
= wxMacFSRefToPath( &theRef
) ;
191 // AEPDoc Calls MacPrintFile on each of the files passed
193 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
197 DescType returnedType
;
202 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
206 err
= AECountItems(&docList
, &itemsInList
);
210 ProcessSerialNumber PSN
;
211 PSN
.highLongOfPSN
= 0 ;
212 PSN
.lowLongOfPSN
= kCurrentProcess
;
213 SetFrontProcess( &PSN
) ;
215 for (i
= 1; i
<= itemsInList
; i
++) {
219 AEGetNthPtr(&docList
, i
, typeFSRef
, &keywd
, &returnedType
,
220 (Ptr
) & theRef
, sizeof(theRef
), &actualSize
);
221 fName
= wxMacFSRefToPath( &theRef
) ;
228 // AEOApp calls MacNewFile
230 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
236 // AEQuit attempts to quit the application
238 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
240 wxWindow
* win
= GetTopWindow() ;
243 wxCommandEvent
exitEvent(wxEVT_COMMAND_MENU_SELECTED
, s_macExitMenuItemId
);
244 if (!win
->ProcessEvent(exitEvent
))
254 // AEROApp calls MacReopenApp
256 short wxApp::MacHandleAERApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
264 //----------------------------------------------------------------------
265 // Support Routines linking the Mac...File Calls to the Document Manager
266 //----------------------------------------------------------------------
268 void wxApp::MacOpenFile(const wxString
& fileName
)
270 #if wxUSE_DOC_VIEW_ARCHITECTURE
271 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
273 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
278 void wxApp::MacPrintFile(const wxString
& fileName
)
280 #if wxUSE_DOC_VIEW_ARCHITECTURE
282 #if wxUSE_PRINTING_ARCHITECTURE
283 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
286 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
289 wxView
* view
= doc
->GetFirstView() ;
292 wxPrintout
*printout
= view
->OnCreatePrintout();
296 printer
.Print(view
->GetFrame(), printout
, true);
302 doc
->DeleteAllViews();
303 dm
->RemoveDocument(doc
) ;
314 void wxApp::MacNewFile()
318 void wxApp::MacReopenApp()
321 // if there is no open window -> create a new one
322 // if all windows are hidden -> show the first
323 // if some windows are not hidden -> do nothing
325 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
332 wxTopLevelWindow
* firstIconized
= NULL
;
335 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->GetData();
336 if ( win
->IsIconized() == false )
338 firstIconized
= NULL
;
343 if ( firstIconized
== NULL
)
344 firstIconized
= win
;
346 node
= node
->GetNext();
349 firstIconized
->Iconize( false ) ;
353 //----------------------------------------------------------------------
354 // Carbon Event Handler
355 //----------------------------------------------------------------------
357 static const EventTypeSpec eventList
[] =
359 { kEventClassCommand
, kEventProcessCommand
} ,
360 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
362 { kEventClassMenu
, kEventMenuOpening
},
363 { kEventClassMenu
, kEventMenuClosed
},
364 { kEventClassMenu
, kEventMenuTargetItem
},
366 { kEventClassApplication
, kEventAppActivated
} ,
367 { kEventClassApplication
, kEventAppDeactivated
} ,
368 // handling the quit event is not recommended by apple
369 // rather using the quit apple event - which we do
371 { kEventClassAppleEvent
, kEventAppleEvent
} ,
373 { kEventClassMouse
, kEventMouseDown
} ,
374 { kEventClassMouse
, kEventMouseMoved
} ,
375 { kEventClassMouse
, kEventMouseUp
} ,
376 { kEventClassMouse
, kEventMouseDragged
} ,
380 static pascal OSStatus
381 wxMacAppMenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
383 wxMacCarbonEvent
cEvent( event
) ;
384 MenuRef menuRef
= cEvent
.GetParameter
<MenuRef
>(kEventParamDirectObject
) ;
385 wxMenu
* menu
= wxFindMenuFromMacMenu( menuRef
) ;
391 switch (GetEventKind(event
))
393 case kEventMenuOpening
:
394 type
= wxEVT_MENU_OPEN
;
396 case kEventMenuClosed
:
397 type
= wxEVT_MENU_CLOSE
;
399 case kEventMenuTargetItem
:
400 cmd
= cEvent
.GetParameter
<MenuCommand
>(kEventParamMenuCommand
,typeMenuCommand
) ;
402 type
= wxEVT_MENU_HIGHLIGHT
;
405 wxFAIL_MSG(wxT("Unexpected menu event kind"));
411 wxMenuEvent
wxevent(type
, cmd
, menu
);
412 wxevent
.SetEventObject(menu
);
414 wxEvtHandler
* handler
= menu
->GetEventHandler();
415 if (handler
&& handler
->ProcessEvent(wxevent
))
421 wxWindow
*win
= menu
->GetInvokingWindow();
423 win
->GetEventHandler()->ProcessEvent(wxevent
);
428 return eventNotHandledErr
;
431 static pascal OSStatus
wxMacAppCommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
433 OSStatus result
= eventNotHandledErr
;
437 wxMacCarbonEvent
cEvent( event
) ;
438 cEvent
.GetParameter
<HICommand
>(kEventParamDirectObject
,typeHICommand
,&command
) ;
440 wxMenuItem
* item
= NULL
;
441 MenuCommand id
= command
.commandID
;
442 // for items we don't really control
443 if ( id
== kHICommandPreferences
)
445 id
= wxApp::s_macPreferencesMenuItemId
;
447 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
450 wxMenu
* menu
= NULL
;
451 item
= mbar
->FindItem( id
, &menu
) ;
454 else if ( id
!= 0 && command
.menu
.menuRef
!= 0 && command
.menu
.menuItemIndex
!= 0 )
456 wxMenu
* itsMenu
= NULL
;
458 GetMenuItemRefCon( command
.menu
.menuRef
, command
.menu
.menuItemIndex
, &refCon
) ;
459 // make sure it is one of our own menus, otherwise don't touch
460 itsMenu
= wxFindMenuFromMacMenu( command
.menu
.menuRef
) ;
461 if ( itsMenu
!= NULL
)
463 item
= (wxMenuItem
*) refCon
;
469 switch( cEvent
.GetKind() )
471 case kEventProcessCommand
:
473 if (item
->IsCheckable())
475 item
->Check( !item
->IsChecked() ) ;
478 item
->GetMenu()->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
482 case kEventCommandUpdateStatus
:
483 // eventually trigger an updateui round
493 static pascal OSStatus
wxMacAppApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
495 OSStatus result
= eventNotHandledErr
;
496 switch ( GetEventKind( event
) )
498 case kEventAppActivated
:
501 wxTheApp
->SetActive( true , NULL
) ;
505 case kEventAppDeactivated
:
508 wxTheApp
->SetActive( false , NULL
) ;
518 pascal OSStatus
wxMacAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
520 EventRef formerEvent
= (EventRef
) wxTheApp
->MacGetCurrentEvent() ;
521 EventHandlerCallRef formerEventHandlerCallRef
= (EventHandlerCallRef
) wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
522 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
524 OSStatus result
= eventNotHandledErr
;
525 switch( GetEventClass( event
) )
527 case kEventClassCommand
:
528 result
= wxMacAppCommandEventHandler( handler
, event
, data
) ;
530 case kEventClassApplication
:
531 result
= wxMacAppApplicationEventHandler( handler
, event
, data
) ;
533 case kEventClassMenu
:
534 result
= wxMacAppMenuEventHandler( handler
, event
, data
) ;
536 case kEventClassMouse
:
538 wxMacCarbonEvent
cEvent( event
) ;
541 Point screenMouseLocation
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ;
542 ::FindWindow(screenMouseLocation
, &window
);
543 // only send this event in case it had not already been sent to a tlw, as we get
544 // double events otherwise (in case event.skip) was called
545 if ( window
== NULL
)
546 result
= wxMacTopLevelMouseEventHandler( handler
, event
, NULL
) ;
549 case kEventClassAppleEvent
:
552 wxMacConvertEventToRecord( event
, &rec
) ;
553 result
= AEProcessAppleEvent( &rec
) ;
560 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerEventHandlerCallRef
) ;
565 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler
)
567 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
568 // we know it's there ;-)
569 WXIMPORT
char std::__throws_bad_alloc
;
574 pascal static void wxMacAssertOutputHandler(OSType componentSignature
, UInt32 options
,
575 const char *assertionString
, const char *exceptionLabelString
,
576 const char *errorString
, const char *fileName
, long lineNumber
, void *value
, ConstStr255Param outputMsg
)
578 // flow into assert handling
579 wxString fileNameStr
;
580 wxString assertionStr
;
581 wxString exceptionStr
;
584 fileNameStr
= wxString(fileName
, wxConvLocal
);
585 assertionStr
= wxString(assertionString
, wxConvLocal
);
586 exceptionStr
= wxString((exceptionLabelString
!=0) ? exceptionLabelString
: "", wxConvLocal
) ;
587 errorStr
= wxString((errorString
!=0) ? errorString
: "", wxConvLocal
) ;
589 fileNameStr
= fileName
;
590 assertionStr
= assertionString
;
591 exceptionStr
= (exceptionLabelString
!=0) ? exceptionLabelString
: "" ;
592 errorStr
= (errorString
!=0) ? errorString
: "" ;
597 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
598 assertionStr
.c_str() ,
599 exceptionStr
.c_str() ,
601 fileNameStr
.c_str(), lineNumber
,
605 wxOnAssert(fileNameStr
, lineNumber
, assertionStr
,
606 wxString::Format( wxT("%s %s value (%p)") ,exceptionStr
, errorStr
, value
) ) ;
614 /* m_macEventPosted run loop source callback: */
615 void macPostedEventCallback(void *unused
);
618 void macPostedEventCallback(void *unused
) {
619 wxTheApp
->ProcessPendingEvents(); }
622 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
627 InstallDebugAssertOutputHandler ( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler
) );
629 UMAInitToolbox( 4, sm_isEmbedded
) ;
630 SetEventMask( everyEvent
) ;
631 UMAShowWatchCursor() ;
634 # if __option(profile)
635 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
640 // now avoid exceptions thrown for new (bad_alloc)
641 // FIXME CS for some changes outside wxMac does not compile anymore
643 std::__throws_bad_alloc
= 0 ;
648 s_macCursorRgn
= ::NewRgn() ;
650 // Mac OS X passes a process serial number command line argument when
651 // the application is launched from the Finder. This argument must be
652 // removed from the command line arguments before being handled by the
653 // application (otherwise applications would need to handle it)
656 static const wxChar
*ARG_PSN
= _T("-psn_");
657 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
659 // remove this argument
661 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
665 if ( !wxAppBase::Initialize(argc
, argv
) )
669 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
672 #if TARGET_API_MAC_OSX
673 // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
674 wxString startupCwd
= wxGetCwd() ;
675 if ( startupCwd
== wxT("/") || startupCwd
.Right(15) == wxT("/Contents/MacOS") )
677 CFURLRef url
= CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
678 CFURLRef urlParent
= CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault
, url
) ;
680 CFStringRef path
= CFURLCopyFileSystemPath ( urlParent
, kCFURLPOSIXPathStyle
) ;
681 CFRelease( urlParent
) ;
682 wxString cwd
= wxMacCFStringHolder(path
).AsString(wxLocale::GetSystemEncoding());
683 wxSetWorkingDirectory( cwd
) ;
687 wxMacCreateNotifierTable() ;
690 /* connect posted events to common-mode run loop so that wxPostEvent events
691 are handled even while we're in the menu or on a scrollbar */
692 CFRunLoopSourceContext event_posted_context
= {0};
693 event_posted_context
.perform
= macPostedEventCallback
;
694 m_macEventPosted
= CFRunLoopSourceCreate(NULL
,0,&event_posted_context
);
695 CFRunLoopAddSource(CFRunLoopGetCurrent(), m_macEventPosted
, kCFRunLoopCommonModes
);
698 UMAShowArrowCursor() ;
703 AEEventHandlerUPP sODocHandler
= NULL
;
704 AEEventHandlerUPP sOAppHandler
= NULL
;
705 AEEventHandlerUPP sPDocHandler
= NULL
;
706 AEEventHandlerUPP sRAppHandler
= NULL
;
707 AEEventHandlerUPP sQuitHandler
= NULL
;
709 bool wxApp::OnInitGui()
711 if( !wxAppBase::OnInitGui() )
714 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
718 InstallApplicationEventHandler(
719 GetwxMacAppEventHandlerUPP(),
720 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
725 sODocHandler
= NewAEEventHandlerUPP(AEHandleODoc
) ;
726 sOAppHandler
= NewAEEventHandlerUPP(AEHandleOApp
) ;
727 sPDocHandler
= NewAEEventHandlerUPP(AEHandlePDoc
) ;
728 sRAppHandler
= NewAEEventHandlerUPP(AEHandleRApp
) ;
729 sQuitHandler
= NewAEEventHandlerUPP(AEHandleQuit
) ;
731 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
732 sODocHandler
, 0 , FALSE
) ;
733 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
734 sOAppHandler
, 0 , FALSE
) ;
735 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
736 sPDocHandler
, 0 , FALSE
) ;
737 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
738 sRAppHandler
, 0 , FALSE
) ;
739 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
740 sQuitHandler
, 0 , FALSE
) ;
746 void wxApp::CleanUp()
749 wxToolTip::RemoveToolTips() ;
753 if (m_macEventPosted
)
755 CFRelease(m_macEventPosted
);
757 m_macEventPosted
= NULL
;
760 // One last chance for pending objects to be cleaned up
761 wxTheApp
->DeletePendingObjects();
763 wxMacDestroyNotifierTable() ;
766 # if __option(profile)
767 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
772 UMACleanupToolbox() ;
773 if (s_macCursorRgn
) {
774 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
779 RemoveEventHandler( (EventHandlerRef
)(wxTheApp
->m_macEventHandler
) );
784 AERemoveEventHandler( kCoreEventClass
, kAEOpenDocuments
,
785 sODocHandler
, FALSE
) ;
786 AERemoveEventHandler( kCoreEventClass
, kAEOpenApplication
,
787 sOAppHandler
, FALSE
) ;
788 AERemoveEventHandler( kCoreEventClass
, kAEPrintDocuments
,
789 sPDocHandler
, FALSE
) ;
790 AERemoveEventHandler( kCoreEventClass
, kAEReopenApplication
,
791 sRAppHandler
, FALSE
) ;
792 AERemoveEventHandler( kCoreEventClass
, kAEQuitApplication
,
793 sQuitHandler
, FALSE
) ;
795 DisposeAEEventHandlerUPP( sODocHandler
) ;
796 DisposeAEEventHandlerUPP( sOAppHandler
) ;
797 DisposeAEEventHandlerUPP( sPDocHandler
) ;
798 DisposeAEEventHandlerUPP( sRAppHandler
) ;
799 DisposeAEEventHandlerUPP( sQuitHandler
) ;
802 wxAppBase::CleanUp();
805 //----------------------------------------------------------------------
806 // misc initialization stuff
807 //----------------------------------------------------------------------
809 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
811 // for shared libraries we have to manually get the correct resource
812 // ref num upon initializing and releasing when terminating, therefore
813 // the __wxinitialize and __wxterminate must be used
816 void __sinit(void); /* (generated by linker) */
817 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
818 pascal void __terminate(void);
821 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
823 return __initialize( theInitBlock
) ;
826 pascal void __wxterminate(void)
831 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
833 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
835 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
836 OSStatus err
= noErr
;
839 switch( GetEventClass( event
) )
841 case kEventClassKeyboard
:
844 switch( GetEventKind(event
) )
846 case kEventRawKeyDown
:
847 rec
->what
= keyDown
;
849 case kEventRawKeyRepeat
:
850 rec
->what
= autoKey
;
852 case kEventRawKeyUp
:
855 case kEventRawKeyModifiersChanged
:
856 rec
->what
= nullEvent
;
865 unsigned char charCode
;
867 GetMouse( &rec
->where
) ;
869 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
870 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
871 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
872 rec
->modifiers
= modifiers
;
873 rec
->message
= (keyCode
<< 8 ) + charCode
;
877 case kEventClassTextInput
:
879 switch( GetEventKind( event
) )
881 case kEventTextInputUnicodeForKeyEvent
:
884 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
888 unsigned char charCode
;
890 GetMouse( &rec
->where
) ;
891 rec
->what
= keyDown
;
892 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
893 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
894 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
895 rec
->modifiers
= modifiers
;
896 rec
->message
= (keyCode
<< 8 ) + charCode
;
913 m_printMode
= wxPRINT_WINDOWS
;
915 m_macCurrentEvent
= NULL
;
916 m_macCurrentEventHandlerCallRef
= NULL
;
918 m_macEventPosted
= NULL
;
922 int wxApp::MainLoop()
926 RunApplicationEventLoop() ;
936 void wxApp::ExitMainLoop()
940 QuitApplicationEventLoop() ;
944 // Is a message/event pending?
945 bool wxApp::Pending()
947 // without the receive event (with pull param = false ) nothing is ever reported
949 ReceiveNextEvent (0, NULL
, kEventDurationNoWait
, false, &theEvent
);
950 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
953 // Dispatch a message.
954 bool wxApp::Dispatch()
961 void wxApp::OnIdle(wxIdleEvent
& event
)
963 wxAppBase::OnIdle(event
);
965 // If they are pending events, we must process them: pending events are
966 // either events to the threads other than main or events posted with
967 // wxPostEvent() functions
968 wxMacProcessNotifierAndPendingEvents();
970 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
971 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
974 void wxApp::WakeUpIdle()
977 if (m_macEventPosted
)
979 CFRunLoopSourceSignal(m_macEventPosted
);
991 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
994 GetTopWindow()->Close(true);
997 // Default behaviour: close the application with prompts. The
998 // user can veto the close, and therefore the end session.
999 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1003 if (!GetTopWindow()->Close(!event
.CanVeto()))
1008 extern "C" void wxCYield() ;
1014 // Yield to other processes
1016 bool wxApp::Yield(bool onlyIfNeeded
)
1020 if ( !onlyIfNeeded
)
1022 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1030 // by definition yield should handle all non-processed events
1034 OSStatus status
= noErr
;
1037 s_inReceiveEvent
= true ;
1038 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1039 s_inReceiveEvent
= false ;
1041 if ( status
== eventLoopTimedOutErr
)
1043 // make sure next time the event loop will trigger idle events
1044 sleepTime
= kEventDurationNoWait
;
1046 else if ( status
== eventLoopQuitErr
)
1048 // according to QA1061 this may also occur when a WakeUp Process
1053 MacHandleOneEvent( theEvent
) ;
1054 ReleaseEvent(theEvent
);
1056 } while( status
== noErr
) ;
1058 wxMacProcessNotifierAndPendingEvents() ;
1064 void wxApp::MacDoOneEvent()
1068 s_inReceiveEvent
= true ;
1069 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1070 s_inReceiveEvent
= false ;
1071 if ( status
== eventLoopTimedOutErr
)
1073 if ( wxTheApp
->ProcessIdle() )
1074 sleepTime
= kEventDurationNoWait
;
1076 sleepTime
= kEventDurationSecond
;
1078 else if ( status
== eventLoopQuitErr
)
1080 // according to QA1061 this may also occur when a WakeUp Process
1085 MacHandleOneEvent( theEvent
) ;
1086 ReleaseEvent(theEvent
);
1087 sleepTime
= kEventDurationNoWait
;
1091 DeletePendingObjects() ;
1092 wxMacProcessNotifierAndPendingEvents() ;
1095 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1097 // Override to process unhandled events as you please
1100 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1102 EventTargetRef theTarget
;
1103 theTarget
= GetEventDispatcherTarget();
1104 m_macCurrentEvent
= evr
;
1105 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1106 if(status
== eventNotHandledErr
)
1108 MacHandleUnhandledEvent(evr
);
1110 wxMacProcessNotifierAndPendingEvents() ;
1112 wxMutexGuiLeaveOrEnter();
1113 #endif // wxUSE_THREADS
1116 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1117 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1122 case kHomeCharCode
:
1126 case kEnterCharCode
:
1127 retval
= WXK_RETURN
;
1133 case kHelpCharCode
:
1137 case kBackspaceCharCode
:
1145 case kPageUpCharCode
:
1146 retval
= WXK_PAGEUP
;
1149 case kPageDownCharCode
:
1150 retval
= WXK_PAGEDOWN
;
1153 case kReturnCharCode
:
1154 retval
= WXK_RETURN
;
1157 case kFunctionKeyCharCode
:
1210 case kEscapeCharCode
:
1211 retval
= WXK_ESCAPE
;
1214 case kLeftArrowCharCode
:
1218 case kRightArrowCharCode
:
1219 retval
= WXK_RIGHT
;
1222 case kUpArrowCharCode
:
1226 case kDownArrowCharCode
:
1230 case kDeleteCharCode
:
1231 retval
= WXK_DELETE
;
1241 int wxMacKeyCodeToModifier(wxKeyCode key
)
1267 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1269 wxASSERT_MSG(key
!= WXK_LBUTTON
&& key
!= WXK_RBUTTON
&& key
!=
1270 WXK_MBUTTON
, wxT("can't use wxGetKeyState() for mouse buttons"));
1272 //if OS X > 10.2 (i.e. 10.2.x)
1273 //a known apple bug prevents the system from determining led
1274 //states with GetKeys... can only determine caps lock led
1275 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key
));
1277 // KeyMapByteArray keymap;
1278 // GetKeys((BigEndianLong*)keymap);
1279 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1284 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1291 keychar
= short(keymessage
& charCodeMask
);
1292 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1294 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1296 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1297 // and look at the character after
1299 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1300 keychar
= short(keyInfo
& charCodeMask
);
1302 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1303 long realkeyval
= keyval
;
1304 if ( keyval
== keychar
)
1306 // 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)
1307 realkeyval
= short(keymessage
& charCodeMask
) ;
1308 keyval
= wxToupper( keyval
) ;
1311 // Check for NUMPAD keys
1312 if (keyval
>= '0' && keyval
<= '9' && keycode
>= 82 && keycode
<= 92)
1314 keyval
= keyval
- '0' + WXK_NUMPAD0
;
1316 else if (keycode
>= 67 && keycode
<= 81)
1321 keyval
= WXK_NUMPAD_ENTER
;
1324 keyval
= WXK_NUMPAD_EQUAL
;
1327 keyval
= WXK_NUMPAD_MULTIPLY
;
1330 keyval
= WXK_NUMPAD_DIVIDE
;
1333 keyval
= WXK_NUMPAD_SUBTRACT
;
1336 keyval
= WXK_NUMPAD_ADD
;
1339 keyval
= WXK_NUMPAD_DECIMAL
;
1344 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1345 bool handled
= false ;
1346 event
.m_shiftDown
= modifiers
& shiftKey
;
1347 event
.m_controlDown
= modifiers
& controlKey
;
1348 event
.m_altDown
= modifiers
& optionKey
;
1349 event
.m_metaDown
= modifiers
& cmdKey
;
1350 event
.m_keyCode
= keyval
;
1352 event
.m_uniChar
= uniChar
;
1354 event
.m_rawCode
= keymessage
;
1355 event
.m_rawFlags
= modifiers
;
1358 event
.SetTimestamp(when
);
1359 event
.SetEventObject(focus
);
1360 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1361 if ( handled
&& event
.GetSkipped() )
1367 wxWindow
*ancestor
= focus
;
1370 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1373 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1374 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1377 if (ancestor
->IsTopLevel())
1379 ancestor
= ancestor
->GetParent();
1382 #endif // wxUSE_ACCEL
1386 wxTopLevelWindowMac
*tlw
= focus
->MacGetTopLevelWindow() ;
1390 event
.Skip( false ) ;
1391 event
.SetEventType( wxEVT_CHAR_HOOK
);
1393 event
.m_keyCode
= realkeyval
;
1395 handled
= tlw
->GetEventHandler()->ProcessEvent( event
);
1396 if ( handled
&& event
.GetSkipped() )
1403 event
.Skip( false ) ;
1404 event
.SetEventType( wxEVT_CHAR
) ;
1406 event
.m_keyCode
= realkeyval
;
1408 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1409 if ( handled
&& event
.GetSkipped() )
1412 if ( !handled
&& (keyval
== WXK_TAB
) )
1414 wxWindow
* iter
= focus
->GetParent() ;
1415 while( iter
&& !handled
)
1417 if ( iter
->HasFlag( wxTAB_TRAVERSAL
) )
1419 wxNavigationKeyEvent new_event
;
1420 new_event
.SetEventObject( focus
);
1421 new_event
.SetDirection( !event
.ShiftDown() );
1422 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1423 new_event
.SetWindowChange( event
.ControlDown() );
1424 new_event
.SetCurrentFocus( focus
);
1425 handled
= focus
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
1426 if ( handled
&& new_event
.GetSkipped() )
1429 iter
= iter
->GetParent() ;
1432 // backdoor handler for default return and command escape
1433 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1435 // if window is not having a focus still testing for default enter or cancel
1436 // TODO add the UMA version for ActiveNonFloatingWindow
1437 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1440 if ( keyval
== WXK_RETURN
)
1442 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1444 if ( def
&& def
->IsEnabled() )
1446 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1447 event
.SetEventObject(def
);
1448 def
->Command(event
);
1452 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1453 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1455 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1456 new_event
.SetEventObject( focus
);
1457 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1464 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1471 keychar
= short(keymessage
& charCodeMask
);
1472 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1473 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1475 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1476 // and look at the character after
1478 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1479 keychar
= short(keyInfo
& charCodeMask
);
1481 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1483 if ( keyval
== keychar
)
1485 keyval
= wxToupper( keyval
) ;
1488 // Check for NUMPAD keys
1489 if (keyval
>= '0' && keyval
<= '9' && keycode
>= 82 && keycode
<= 92)
1491 keyval
= keyval
- '0' + WXK_NUMPAD0
;
1493 else if (keycode
>= 67 && keycode
<= 81)
1498 keyval
= WXK_NUMPAD_ENTER
;
1501 keyval
= WXK_NUMPAD_EQUAL
;
1504 keyval
= WXK_NUMPAD_MULTIPLY
;
1507 keyval
= WXK_NUMPAD_DIVIDE
;
1510 keyval
= WXK_NUMPAD_SUBTRACT
;
1513 keyval
= WXK_NUMPAD_ADD
;
1516 keyval
= WXK_NUMPAD_DECIMAL
;
1521 bool handled
= false ;
1523 wxKeyEvent
event(wxEVT_KEY_UP
);
1524 event
.m_shiftDown
= modifiers
& shiftKey
;
1525 event
.m_controlDown
= modifiers
& controlKey
;
1526 event
.m_altDown
= modifiers
& optionKey
;
1527 event
.m_metaDown
= modifiers
& cmdKey
;
1528 event
.m_keyCode
= keyval
;
1530 event
.m_uniChar
= uniChar
;
1533 event
.m_rawCode
= keymessage
;
1534 event
.m_rawFlags
= modifiers
;
1537 event
.SetTimestamp(when
);
1538 event
.SetEventObject(focus
);
1539 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;