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 GetMenuItemRefCon( command
.menu
.menuRef
, command
.menu
.menuItemIndex
, (UInt32
*) &item
) ;
461 switch( cEvent
.GetKind() )
463 case kEventProcessCommand
:
465 if (item
->IsCheckable())
467 item
->Check( !item
->IsChecked() ) ;
470 item
->GetMenu()->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
474 case kEventCommandUpdateStatus
:
475 // eventually trigger an updateui round
485 static pascal OSStatus
wxMacAppApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
487 OSStatus result
= eventNotHandledErr
;
488 switch ( GetEventKind( event
) )
490 case kEventAppActivated
:
493 wxTheApp
->SetActive( true , NULL
) ;
497 case kEventAppDeactivated
:
500 wxTheApp
->SetActive( false , NULL
) ;
510 pascal OSStatus
wxMacAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
512 EventRef formerEvent
= (EventRef
) wxTheApp
->MacGetCurrentEvent() ;
513 EventHandlerCallRef formerEventHandlerCallRef
= (EventHandlerCallRef
) wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
514 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
516 OSStatus result
= eventNotHandledErr
;
517 switch( GetEventClass( event
) )
519 case kEventClassCommand
:
520 result
= wxMacAppCommandEventHandler( handler
, event
, data
) ;
522 case kEventClassApplication
:
523 result
= wxMacAppApplicationEventHandler( handler
, event
, data
) ;
525 case kEventClassMenu
:
526 result
= wxMacAppMenuEventHandler( handler
, event
, data
) ;
528 case kEventClassMouse
:
530 wxMacCarbonEvent
cEvent( event
) ;
533 Point screenMouseLocation
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ;
534 ::FindWindow(screenMouseLocation
, &window
);
535 // only send this event in case it had not already been sent to a tlw, as we get
536 // double events otherwise (in case event.skip) was called
537 if ( window
== NULL
)
538 result
= wxMacTopLevelMouseEventHandler( handler
, event
, NULL
) ;
541 case kEventClassAppleEvent
:
544 wxMacConvertEventToRecord( event
, &rec
) ;
545 result
= AEProcessAppleEvent( &rec
) ;
552 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerEventHandlerCallRef
) ;
557 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler
)
559 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
560 // we know it's there ;-)
561 WXIMPORT
char std::__throws_bad_alloc
;
566 pascal static void wxMacAssertOutputHandler(OSType componentSignature
, UInt32 options
,
567 const char *assertionString
, const char *exceptionLabelString
,
568 const char *errorString
, const char *fileName
, long lineNumber
, void *value
, ConstStr255Param outputMsg
)
570 // flow into assert handling
571 wxString fileNameStr
;
572 wxString assertionStr
;
573 wxString exceptionStr
;
576 fileNameStr
= wxString(fileName
, wxConvLocal
);
577 assertionStr
= wxString(assertionString
, wxConvLocal
);
578 exceptionStr
= wxString((exceptionLabelString
!=0) ? exceptionLabelString
: "", wxConvLocal
) ;
579 errorStr
= wxString((errorString
!=0) ? errorString
: "", wxConvLocal
) ;
581 fileNameStr
= fileName
;
582 assertionStr
= assertionString
;
583 exceptionStr
= (exceptionLabelString
!=0) ? exceptionLabelString
: "" ;
584 errorStr
= (errorString
!=0) ? errorString
: "" ;
589 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
590 assertionStr
.c_str() ,
591 exceptionStr
.c_str() ,
593 fileNameStr
.c_str(), lineNumber
,
597 wxOnAssert(fileNameStr
, lineNumber
, assertionStr
,
598 wxString::Format( wxT("%s %s value (%p)") ,exceptionStr
, errorStr
, value
) ) ;
606 /* m_macEventPosted run loop source callback: */
607 void macPostedEventCallback(void *unused
);
610 void macPostedEventCallback(void *unused
) {
611 wxTheApp
->ProcessPendingEvents(); }
614 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
619 InstallDebugAssertOutputHandler ( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler
) );
621 UMAInitToolbox( 4, sm_isEmbedded
) ;
622 SetEventMask( everyEvent
) ;
623 UMAShowWatchCursor() ;
626 # if __option(profile)
627 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
632 // now avoid exceptions thrown for new (bad_alloc)
633 // FIXME CS for some changes outside wxMac does not compile anymore
635 std::__throws_bad_alloc
= 0 ;
640 s_macCursorRgn
= ::NewRgn() ;
642 // Mac OS X passes a process serial number command line argument when
643 // the application is launched from the Finder. This argument must be
644 // removed from the command line arguments before being handled by the
645 // application (otherwise applications would need to handle it)
648 static const wxChar
*ARG_PSN
= _T("-psn_");
649 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
651 // remove this argument
653 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
657 if ( !wxAppBase::Initialize(argc
, argv
) )
661 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
664 #if TARGET_API_MAC_OSX
665 // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
666 wxString startupCwd
= wxGetCwd() ;
667 if ( startupCwd
== wxT("/") || startupCwd
.Right(15) == wxT("/Contents/MacOS") )
669 CFURLRef url
= CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
670 CFURLRef urlParent
= CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault
, url
) ;
672 CFStringRef path
= CFURLCopyFileSystemPath ( urlParent
, kCFURLPOSIXPathStyle
) ;
673 CFRelease( urlParent
) ;
674 wxString cwd
= wxMacCFStringHolder(path
).AsString(wxLocale::GetSystemEncoding());
675 wxSetWorkingDirectory( cwd
) ;
679 wxMacCreateNotifierTable() ;
682 /* connect posted events to common-mode run loop so that wxPostEvent events
683 are handled even while we're in the menu or on a scrollbar */
684 CFRunLoopSourceContext event_posted_context
= {0};
685 event_posted_context
.perform
= macPostedEventCallback
;
686 m_macEventPosted
= CFRunLoopSourceCreate(NULL
,0,&event_posted_context
);
687 CFRunLoopAddSource(CFRunLoopGetCurrent(), m_macEventPosted
, kCFRunLoopCommonModes
);
690 UMAShowArrowCursor() ;
695 AEEventHandlerUPP sODocHandler
= NULL
;
696 AEEventHandlerUPP sOAppHandler
= NULL
;
697 AEEventHandlerUPP sPDocHandler
= NULL
;
698 AEEventHandlerUPP sRAppHandler
= NULL
;
699 AEEventHandlerUPP sQuitHandler
= NULL
;
701 bool wxApp::OnInitGui()
703 if( !wxAppBase::OnInitGui() )
706 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
710 InstallApplicationEventHandler(
711 GetwxMacAppEventHandlerUPP(),
712 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
717 sODocHandler
= NewAEEventHandlerUPP(AEHandleODoc
) ;
718 sOAppHandler
= NewAEEventHandlerUPP(AEHandleOApp
) ;
719 sPDocHandler
= NewAEEventHandlerUPP(AEHandlePDoc
) ;
720 sRAppHandler
= NewAEEventHandlerUPP(AEHandleRApp
) ;
721 sQuitHandler
= NewAEEventHandlerUPP(AEHandleQuit
) ;
723 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
724 sODocHandler
, 0 , FALSE
) ;
725 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
726 sOAppHandler
, 0 , FALSE
) ;
727 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
728 sPDocHandler
, 0 , FALSE
) ;
729 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
730 sRAppHandler
, 0 , FALSE
) ;
731 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
732 sQuitHandler
, 0 , FALSE
) ;
738 void wxApp::CleanUp()
741 wxToolTip::RemoveToolTips() ;
745 if (m_macEventPosted
)
747 CFRelease(m_macEventPosted
);
749 m_macEventPosted
= NULL
;
752 // One last chance for pending objects to be cleaned up
753 wxTheApp
->DeletePendingObjects();
755 wxMacDestroyNotifierTable() ;
758 # if __option(profile)
759 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
764 UMACleanupToolbox() ;
765 if (s_macCursorRgn
) {
766 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
771 RemoveEventHandler( (EventHandlerRef
)(wxTheApp
->m_macEventHandler
) );
776 AERemoveEventHandler( kCoreEventClass
, kAEOpenDocuments
,
777 sODocHandler
, FALSE
) ;
778 AERemoveEventHandler( kCoreEventClass
, kAEOpenApplication
,
779 sOAppHandler
, FALSE
) ;
780 AERemoveEventHandler( kCoreEventClass
, kAEPrintDocuments
,
781 sPDocHandler
, FALSE
) ;
782 AERemoveEventHandler( kCoreEventClass
, kAEReopenApplication
,
783 sRAppHandler
, FALSE
) ;
784 AERemoveEventHandler( kCoreEventClass
, kAEQuitApplication
,
785 sQuitHandler
, FALSE
) ;
787 DisposeAEEventHandlerUPP( sODocHandler
) ;
788 DisposeAEEventHandlerUPP( sOAppHandler
) ;
789 DisposeAEEventHandlerUPP( sPDocHandler
) ;
790 DisposeAEEventHandlerUPP( sRAppHandler
) ;
791 DisposeAEEventHandlerUPP( sQuitHandler
) ;
794 wxAppBase::CleanUp();
797 //----------------------------------------------------------------------
798 // misc initialization stuff
799 //----------------------------------------------------------------------
801 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
803 // for shared libraries we have to manually get the correct resource
804 // ref num upon initializing and releasing when terminating, therefore
805 // the __wxinitialize and __wxterminate must be used
808 void __sinit(void); /* (generated by linker) */
809 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
810 pascal void __terminate(void);
813 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
815 return __initialize( theInitBlock
) ;
818 pascal void __wxterminate(void)
823 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
825 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
827 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
828 OSStatus err
= noErr
;
831 switch( GetEventClass( event
) )
833 case kEventClassKeyboard
:
836 switch( GetEventKind(event
) )
838 case kEventRawKeyDown
:
839 rec
->what
= keyDown
;
841 case kEventRawKeyRepeat
:
842 rec
->what
= autoKey
;
844 case kEventRawKeyUp
:
847 case kEventRawKeyModifiersChanged
:
848 rec
->what
= nullEvent
;
857 unsigned char charCode
;
859 GetMouse( &rec
->where
) ;
861 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
862 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
863 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
864 rec
->modifiers
= modifiers
;
865 rec
->message
= (keyCode
<< 8 ) + charCode
;
869 case kEventClassTextInput
:
871 switch( GetEventKind( event
) )
873 case kEventTextInputUnicodeForKeyEvent
:
876 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
880 unsigned char charCode
;
882 GetMouse( &rec
->where
) ;
883 rec
->what
= keyDown
;
884 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
885 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
886 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
887 rec
->modifiers
= modifiers
;
888 rec
->message
= (keyCode
<< 8 ) + charCode
;
905 m_printMode
= wxPRINT_WINDOWS
;
907 m_macCurrentEvent
= NULL
;
908 m_macCurrentEventHandlerCallRef
= NULL
;
910 m_macEventPosted
= NULL
;
914 int wxApp::MainLoop()
918 RunApplicationEventLoop() ;
928 void wxApp::ExitMainLoop()
932 QuitApplicationEventLoop() ;
936 // Is a message/event pending?
937 bool wxApp::Pending()
939 // without the receive event (with pull param = false ) nothing is ever reported
941 ReceiveNextEvent (0, NULL
, kEventDurationNoWait
, false, &theEvent
);
942 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
945 // Dispatch a message.
946 bool wxApp::Dispatch()
953 void wxApp::OnIdle(wxIdleEvent
& event
)
955 wxAppBase::OnIdle(event
);
957 // If they are pending events, we must process them: pending events are
958 // either events to the threads other than main or events posted with
959 // wxPostEvent() functions
960 wxMacProcessNotifierAndPendingEvents();
962 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
963 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
966 void wxApp::WakeUpIdle()
969 if (m_macEventPosted
)
971 CFRunLoopSourceSignal(m_macEventPosted
);
983 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
986 GetTopWindow()->Close(true);
989 // Default behaviour: close the application with prompts. The
990 // user can veto the close, and therefore the end session.
991 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
995 if (!GetTopWindow()->Close(!event
.CanVeto()))
1000 extern "C" void wxCYield() ;
1006 // Yield to other processes
1008 bool wxApp::Yield(bool onlyIfNeeded
)
1012 if ( !onlyIfNeeded
)
1014 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1022 // by definition yield should handle all non-processed events
1026 OSStatus status
= noErr
;
1029 s_inReceiveEvent
= true ;
1030 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1031 s_inReceiveEvent
= false ;
1033 if ( status
== eventLoopTimedOutErr
)
1035 // make sure next time the event loop will trigger idle events
1036 sleepTime
= kEventDurationNoWait
;
1038 else if ( status
== eventLoopQuitErr
)
1040 // according to QA1061 this may also occur when a WakeUp Process
1045 MacHandleOneEvent( theEvent
) ;
1046 ReleaseEvent(theEvent
);
1048 } while( status
== noErr
) ;
1050 wxMacProcessNotifierAndPendingEvents() ;
1056 void wxApp::MacDoOneEvent()
1060 s_inReceiveEvent
= true ;
1061 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1062 s_inReceiveEvent
= false ;
1063 if ( status
== eventLoopTimedOutErr
)
1065 if ( wxTheApp
->ProcessIdle() )
1066 sleepTime
= kEventDurationNoWait
;
1068 sleepTime
= kEventDurationSecond
;
1070 else if ( status
== eventLoopQuitErr
)
1072 // according to QA1061 this may also occur when a WakeUp Process
1077 MacHandleOneEvent( theEvent
) ;
1078 ReleaseEvent(theEvent
);
1079 sleepTime
= kEventDurationNoWait
;
1083 DeletePendingObjects() ;
1084 wxMacProcessNotifierAndPendingEvents() ;
1087 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1089 // Override to process unhandled events as you please
1092 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1094 EventTargetRef theTarget
;
1095 theTarget
= GetEventDispatcherTarget();
1096 m_macCurrentEvent
= evr
;
1097 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1098 if(status
== eventNotHandledErr
)
1100 MacHandleUnhandledEvent(evr
);
1102 wxMacProcessNotifierAndPendingEvents() ;
1104 wxMutexGuiLeaveOrEnter();
1105 #endif // wxUSE_THREADS
1108 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1109 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1114 case kHomeCharCode
:
1118 case kEnterCharCode
:
1119 retval
= WXK_RETURN
;
1125 case kHelpCharCode
:
1129 case kBackspaceCharCode
:
1137 case kPageUpCharCode
:
1138 retval
= WXK_PAGEUP
;
1141 case kPageDownCharCode
:
1142 retval
= WXK_PAGEDOWN
;
1145 case kReturnCharCode
:
1146 retval
= WXK_RETURN
;
1149 case kFunctionKeyCharCode
:
1202 case kEscapeCharCode
:
1203 retval
= WXK_ESCAPE
;
1206 case kLeftArrowCharCode
:
1210 case kRightArrowCharCode
:
1211 retval
= WXK_RIGHT
;
1214 case kUpArrowCharCode
:
1218 case kDownArrowCharCode
:
1222 case kDeleteCharCode
:
1223 retval
= WXK_DELETE
;
1233 int wxMacKeyCodeToModifier(wxKeyCode key
)
1259 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1261 wxASSERT_MSG(key
!= WXK_LBUTTON
&& key
!= WXK_RBUTTON
&& key
!=
1262 WXK_MBUTTON
, wxT("can't use wxGetKeyState() for mouse buttons"));
1264 //if OS X > 10.2 (i.e. 10.2.x)
1265 //a known apple bug prevents the system from determining led
1266 //states with GetKeys... can only determine caps lock led
1267 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key
));
1269 // KeyMapByteArray keymap;
1270 // GetKeys((BigEndianLong*)keymap);
1271 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1276 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1283 keychar
= short(keymessage
& charCodeMask
);
1284 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1286 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1288 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1289 // and look at the character after
1291 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1292 keychar
= short(keyInfo
& charCodeMask
);
1294 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1295 long realkeyval
= keyval
;
1296 if ( keyval
== keychar
)
1298 // 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)
1299 realkeyval
= short(keymessage
& charCodeMask
) ;
1300 keyval
= wxToupper( keyval
) ;
1303 // Check for NUMPAD keys
1304 if (keyval
>= '0' && keyval
<= '9' && keycode
>= 82 && keycode
<= 92)
1306 keyval
= keyval
- '0' + WXK_NUMPAD0
;
1308 else if (keycode
>= 67 && keycode
<= 81)
1313 keyval
= WXK_NUMPAD_ENTER
;
1316 keyval
= WXK_NUMPAD_EQUAL
;
1319 keyval
= WXK_NUMPAD_MULTIPLY
;
1322 keyval
= WXK_NUMPAD_DIVIDE
;
1325 keyval
= WXK_NUMPAD_SUBTRACT
;
1328 keyval
= WXK_NUMPAD_ADD
;
1331 keyval
= WXK_NUMPAD_DECIMAL
;
1336 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1337 bool handled
= false ;
1338 event
.m_shiftDown
= modifiers
& shiftKey
;
1339 event
.m_controlDown
= modifiers
& controlKey
;
1340 event
.m_altDown
= modifiers
& optionKey
;
1341 event
.m_metaDown
= modifiers
& cmdKey
;
1342 event
.m_keyCode
= keyval
;
1344 event
.m_uniChar
= uniChar
;
1346 event
.m_rawCode
= keymessage
;
1347 event
.m_rawFlags
= modifiers
;
1350 event
.SetTimestamp(when
);
1351 event
.SetEventObject(focus
);
1352 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1353 if ( handled
&& event
.GetSkipped() )
1359 wxWindow
*ancestor
= focus
;
1362 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1365 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1366 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1369 if (ancestor
->IsTopLevel())
1371 ancestor
= ancestor
->GetParent();
1374 #endif // wxUSE_ACCEL
1378 wxTopLevelWindowMac
*tlw
= focus
->MacGetTopLevelWindow() ;
1382 event
.Skip( false ) ;
1383 event
.SetEventType( wxEVT_CHAR_HOOK
);
1385 event
.m_keyCode
= realkeyval
;
1387 handled
= tlw
->GetEventHandler()->ProcessEvent( event
);
1388 if ( handled
&& event
.GetSkipped() )
1395 event
.Skip( false ) ;
1396 event
.SetEventType( wxEVT_CHAR
) ;
1398 event
.m_keyCode
= realkeyval
;
1400 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1401 if ( handled
&& event
.GetSkipped() )
1404 if ( !handled
&& (keyval
== WXK_TAB
) )
1406 wxWindow
* iter
= focus
->GetParent() ;
1407 while( iter
&& !handled
)
1409 if ( iter
->HasFlag( wxTAB_TRAVERSAL
) )
1411 wxNavigationKeyEvent new_event
;
1412 new_event
.SetEventObject( focus
);
1413 new_event
.SetDirection( !event
.ShiftDown() );
1414 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1415 new_event
.SetWindowChange( event
.ControlDown() );
1416 new_event
.SetCurrentFocus( focus
);
1417 handled
= focus
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
1418 if ( handled
&& new_event
.GetSkipped() )
1421 iter
= iter
->GetParent() ;
1424 // backdoor handler for default return and command escape
1425 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1427 // if window is not having a focus still testing for default enter or cancel
1428 // TODO add the UMA version for ActiveNonFloatingWindow
1429 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1432 if ( keyval
== WXK_RETURN
)
1434 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1436 if ( def
&& def
->IsEnabled() )
1438 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1439 event
.SetEventObject(def
);
1440 def
->Command(event
);
1444 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1445 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1447 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1448 new_event
.SetEventObject( focus
);
1449 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1456 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1463 keychar
= short(keymessage
& charCodeMask
);
1464 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1465 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1467 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1468 // and look at the character after
1470 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1471 keychar
= short(keyInfo
& charCodeMask
);
1473 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1475 if ( keyval
== keychar
)
1477 keyval
= wxToupper( keyval
) ;
1480 // Check for NUMPAD keys
1481 if (keyval
>= '0' && keyval
<= '9' && keycode
>= 82 && keycode
<= 92)
1483 keyval
= keyval
- '0' + WXK_NUMPAD0
;
1485 else if (keycode
>= 67 && keycode
<= 81)
1490 keyval
= WXK_NUMPAD_ENTER
;
1493 keyval
= WXK_NUMPAD_EQUAL
;
1496 keyval
= WXK_NUMPAD_MULTIPLY
;
1499 keyval
= WXK_NUMPAD_DIVIDE
;
1502 keyval
= WXK_NUMPAD_SUBTRACT
;
1505 keyval
= WXK_NUMPAD_ADD
;
1508 keyval
= WXK_NUMPAD_DECIMAL
;
1513 bool handled
= false ;
1515 wxKeyEvent
event(wxEVT_KEY_UP
);
1516 event
.m_shiftDown
= modifiers
& shiftKey
;
1517 event
.m_controlDown
= modifiers
& controlKey
;
1518 event
.m_altDown
= modifiers
& optionKey
;
1519 event
.m_metaDown
= modifiers
& cmdKey
;
1520 event
.m_keyCode
= keyval
;
1522 event
.m_uniChar
= uniChar
;
1525 event
.m_rawCode
= keymessage
;
1526 event
.m_rawFlags
= modifiers
;
1529 event
.SetTimestamp(when
);
1530 event
.SetEventObject(focus
);
1531 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;