1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "app.h"
16 #include "wx/wxprec.h"
18 #include "wx/window.h"
20 #include "wx/button.h"
23 #include "wx/gdicmn.h"
26 #include "wx/cursor.h"
29 #include "wx/palette.h"
31 #include "wx/dialog.h"
32 #include "wx/msgdlg.h"
34 #include "wx/module.h"
35 #include "wx/memory.h"
36 #include "wx/tooltip.h"
37 #include "wx/textctrl.h"
39 #include "wx/docview.h"
40 #include "wx/filename.h"
52 // #include "apprsrc.h"
54 #include "wx/mac/uma.h"
55 #include "wx/mac/macnotfy.h"
58 # include <CoreServices/CoreServices.h>
59 # if defined(WXMAKINGDLL_CORE)
60 # include <mach-o/dyld.h>
65 # include <ToolUtils.h>
66 # include <DiskInit.h>
70 extern wxList wxPendingDelete
;
72 // set wxMAC_USE_RAEL to 1 if RunApplicationEventLoop should be used
73 // if 0 the lower level CarbonEventLoop will be used
74 // on the long run RAEL should replace the low level event loop
75 // we will have to clean up event handling to make sure we don't
76 // miss handling of things like pending events etc
77 // perhaps we will also have to pipe events through an ueber-event-handler
78 // to make sure we have one place to do all these house-keeping functions
80 #define wxMAC_USE_RAEL 0
83 extern size_t g_numberOfThreads
;
84 #endif // wxUSE_THREADS
86 // statics for implementation
88 static bool s_inYield
= FALSE
;
90 static bool s_inReceiveEvent
= FALSE
;
91 static EventTime sleepTime
= kEventDurationNoWait
;
93 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
94 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
95 EVT_IDLE(wxApp::OnIdle
)
96 EVT_END_SESSION(wxApp::OnEndSession
)
97 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
101 const short kMacMinHeap
= (29 * 1024) ;
102 // platform specific static variables
104 const short kwxMacMenuBarResource
= 1 ;
105 const short kwxMacAppleMenuId
= 1 ;
107 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
108 wxWindow
* wxApp::s_captureWindow
= NULL
;
109 int wxApp::s_lastMouseDown
= 0 ;
110 long wxApp::sm_lastMessageTime
= 0;
111 long wxApp::s_lastModifiers
= 0 ;
114 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
115 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
116 long wxApp::s_macPreferencesMenuItemId
= wxID_PREFERENCES
;
117 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
118 wxString
wxApp::s_macHelpMenuTitleName
= wxT("&Help") ;
120 // Normally we're not a plugin
121 bool wxApp::sm_isEmbedded
= false;
122 //----------------------------------------------------------------------
123 // Core Apple Event Support
124 //----------------------------------------------------------------------
126 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
127 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
128 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
129 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
130 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
132 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
134 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
137 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
139 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
142 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
144 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
147 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
149 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
152 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
154 return wxTheApp
->MacHandleAERApp( (AppleEvent
*) event
, reply
) ;
157 // AEODoc Calls MacOpenFile on each of the files passed
159 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
163 DescType returnedType
;
168 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
172 err
= AECountItems(&docList
, &itemsInList
);
176 ProcessSerialNumber PSN
;
177 PSN
.highLongOfPSN
= 0 ;
178 PSN
.lowLongOfPSN
= kCurrentProcess
;
179 SetFrontProcess( &PSN
) ;
181 for (i
= 1; i
<= itemsInList
; i
++)
186 AEGetNthPtr(&docList
, i
, typeFSRef
, &keywd
, &returnedType
,
187 (Ptr
) & theRef
, sizeof(theRef
), &actualSize
);
188 fName
= wxMacFSRefToPath( &theRef
) ;
195 // AEPDoc Calls MacPrintFile on each of the files passed
197 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
201 DescType returnedType
;
206 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
210 err
= AECountItems(&docList
, &itemsInList
);
214 ProcessSerialNumber PSN
;
215 PSN
.highLongOfPSN
= 0 ;
216 PSN
.lowLongOfPSN
= kCurrentProcess
;
217 SetFrontProcess( &PSN
) ;
219 for (i
= 1; i
<= itemsInList
; i
++) {
223 AEGetNthPtr(&docList
, i
, typeFSRef
, &keywd
, &returnedType
,
224 (Ptr
) & theRef
, sizeof(theRef
), &actualSize
);
225 fName
= wxMacFSRefToPath( &theRef
) ;
232 // AEOApp calls MacNewFile
234 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
240 // AEQuit attempts to quit the application
242 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
244 wxWindow
* win
= GetTopWindow() ;
247 wxCommandEvent
exitEvent(wxEVT_COMMAND_MENU_SELECTED
, s_macExitMenuItemId
);
248 if (!win
->ProcessEvent(exitEvent
))
258 // AEROApp calls MacReopenApp
260 short wxApp::MacHandleAERApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
268 //----------------------------------------------------------------------
269 // Support Routines linking the Mac...File Calls to the Document Manager
270 //----------------------------------------------------------------------
272 void wxApp::MacOpenFile(const wxString
& fileName
)
274 #if wxUSE_DOC_VIEW_ARCHITECTURE
275 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
277 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
282 void wxApp::MacPrintFile(const wxString
& fileName
)
284 #if wxUSE_DOC_VIEW_ARCHITECTURE
286 #if wxUSE_PRINTING_ARCHITECTURE
287 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
290 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
293 wxView
* view
= doc
->GetFirstView() ;
296 wxPrintout
*printout
= view
->OnCreatePrintout();
300 printer
.Print(view
->GetFrame(), printout
, TRUE
);
306 doc
->DeleteAllViews();
307 dm
->RemoveDocument(doc
) ;
318 void wxApp::MacNewFile()
322 void wxApp::MacReopenApp()
325 // if there is no open window -> create a new one
326 // if all windows are hidden -> show the first
327 // if some windows are not hidden -> do nothing
329 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
336 wxTopLevelWindow
* firstIconized
= NULL
;
339 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->GetData();
340 if ( win
->IsIconized() == false )
342 firstIconized
= NULL
;
347 if ( firstIconized
== NULL
)
348 firstIconized
= win
;
350 node
= node
->GetNext();
353 firstIconized
->Iconize( false ) ;
357 //----------------------------------------------------------------------
358 // Carbon Event Handler
359 //----------------------------------------------------------------------
361 static const EventTypeSpec eventList
[] =
363 { kEventClassCommand
, kEventProcessCommand
} ,
364 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
366 { kEventClassMenu
, kEventMenuOpening
},
367 { kEventClassMenu
, kEventMenuClosed
},
368 { kEventClassMenu
, kEventMenuTargetItem
},
370 { kEventClassApplication
, kEventAppActivated
} ,
371 { kEventClassApplication
, kEventAppDeactivated
} ,
372 // handling the quit event is not recommended by apple
373 // rather using the quit apple event - which we do
375 { kEventClassAppleEvent
, kEventAppleEvent
} ,
377 { kEventClassMouse
, kEventMouseDown
} ,
378 { kEventClassMouse
, kEventMouseMoved
} ,
379 { kEventClassMouse
, kEventMouseUp
} ,
380 { kEventClassMouse
, kEventMouseDragged
} ,
384 static pascal OSStatus
385 wxMacAppMenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
387 wxMacCarbonEvent
cEvent( event
) ;
388 MenuRef menuRef
= cEvent
.GetParameter
<MenuRef
>(kEventParamDirectObject
) ;
389 wxMenu
* menu
= wxFindMenuFromMacMenu( menuRef
) ;
395 switch (GetEventKind(event
))
397 case kEventMenuOpening
:
398 type
= wxEVT_MENU_OPEN
;
400 case kEventMenuClosed
:
401 type
= wxEVT_MENU_CLOSE
;
403 case kEventMenuTargetItem
:
404 cmd
= cEvent
.GetParameter
<MenuCommand
>(kEventParamMenuCommand
,typeMenuCommand
) ;
406 type
= wxEVT_MENU_HIGHLIGHT
;
409 wxFAIL_MSG(wxT("Unexpected menu event kind"));
415 wxMenuEvent
wxevent(type
, cmd
, menu
);
416 wxevent
.SetEventObject(menu
);
418 wxEvtHandler
* handler
= menu
->GetEventHandler();
419 if (handler
&& handler
->ProcessEvent(wxevent
))
425 wxWindow
*win
= menu
->GetInvokingWindow();
427 win
->GetEventHandler()->ProcessEvent(wxevent
);
432 return eventNotHandledErr
;
435 static pascal OSStatus
wxMacAppCommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
437 OSStatus result
= eventNotHandledErr
;
441 wxMacCarbonEvent
cEvent( event
) ;
442 cEvent
.GetParameter
<HICommand
>(kEventParamDirectObject
,typeHICommand
,&command
) ;
444 wxMenuItem
* item
= NULL
;
445 MenuCommand id
= command
.commandID
;
446 // for items we don't really control
447 if ( id
== kHICommandPreferences
)
449 id
= wxApp::s_macPreferencesMenuItemId
;
451 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
454 wxMenu
* menu
= NULL
;
455 item
= mbar
->FindItem( id
, &menu
) ;
458 else if ( id
!= 0 && command
.menu
.menuRef
!= 0 && command
.menu
.menuItemIndex
!= 0 )
460 GetMenuItemRefCon( command
.menu
.menuRef
, command
.menu
.menuItemIndex
, (UInt32
*) &item
) ;
465 switch( cEvent
.GetKind() )
467 case kEventProcessCommand
:
469 if (item
->IsCheckable())
471 item
->Check( !item
->IsChecked() ) ;
474 item
->GetMenu()->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
478 case kEventCommandUpdateStatus
:
479 // eventually trigger an updateui round
489 static pascal OSStatus
wxMacAppApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
491 OSStatus result
= eventNotHandledErr
;
492 switch ( GetEventKind( event
) )
494 case kEventAppActivated
:
497 wxTheApp
->SetActive( true , NULL
) ;
501 case kEventAppDeactivated
:
504 wxTheApp
->SetActive( false , NULL
) ;
514 pascal OSStatus
wxMacAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
516 EventRef formerEvent
= (EventRef
) wxTheApp
->MacGetCurrentEvent() ;
517 EventHandlerCallRef formerEventHandlerCallRef
= (EventHandlerCallRef
) wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
518 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
520 OSStatus result
= eventNotHandledErr
;
521 switch( GetEventClass( event
) )
523 case kEventClassCommand
:
524 result
= wxMacAppCommandEventHandler( handler
, event
, data
) ;
526 case kEventClassApplication
:
527 result
= wxMacAppApplicationEventHandler( handler
, event
, data
) ;
529 case kEventClassMenu
:
530 result
= wxMacAppMenuEventHandler( handler
, event
, data
) ;
532 case kEventClassMouse
:
534 wxMacCarbonEvent
cEvent( event
) ;
537 Point screenMouseLocation
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ;
538 ::FindWindow(screenMouseLocation
, &window
);
539 // only send this event in case it had not already been sent to a tlw, as we get
540 // double events otherwise (in case event.skip) was called
541 if ( window
== NULL
)
542 result
= wxMacTopLevelMouseEventHandler( handler
, event
, NULL
) ;
545 case kEventClassAppleEvent
:
548 wxMacConvertEventToRecord( event
, &rec
) ;
549 result
= AEProcessAppleEvent( &rec
) ;
556 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerEventHandlerCallRef
) ;
561 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler
)
563 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
564 // we know it's there ;-)
565 WXIMPORT
char std::__throws_bad_alloc
;
570 pascal static void wxMacAssertOutputHandler(OSType componentSignature
, UInt32 options
,
571 const char *assertionString
, const char *exceptionLabelString
,
572 const char *errorString
, const char *fileName
, long lineNumber
, void *value
, ConstStr255Param outputMsg
)
574 // flow into assert handling
575 wxString fileNameStr
;
576 wxString assertionStr
;
577 wxString exceptionStr
;
580 fileNameStr
= wxString(fileName
, wxConvLocal
);
581 assertionStr
= wxString(assertionString
, wxConvLocal
);
582 exceptionStr
= wxString((exceptionLabelString
!=0) ? exceptionLabelString
: "", wxConvLocal
) ;
583 errorStr
= wxString((errorString
!=0) ? errorString
: "", wxConvLocal
) ;
585 fileNameStr
= fileName
;
586 assertionStr
= assertionString
;
587 exceptionStr
= (exceptionLabelString
!=0) ? exceptionLabelString
: "" ;
588 errorStr
= (errorString
!=0) ? errorString
: "" ;
593 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
594 assertionStr
.c_str() ,
595 exceptionStr
.c_str() ,
597 fileNameStr
.c_str(), lineNumber
,
601 wxOnAssert(fileNameStr
, lineNumber
, assertionStr
,
602 wxString::Format( wxT("%s %s value (%p)") ,exceptionStr
, errorStr
, value
) ) ;
610 /* m_macEventPosted run loop source callback: */
611 void macPostedEventCallback(void *unused
);
614 void macPostedEventCallback(void *unused
) {
615 wxTheApp
->ProcessPendingEvents(); }
618 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
623 InstallDebugAssertOutputHandler ( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler
) );
625 UMAInitToolbox( 4, sm_isEmbedded
) ;
626 SetEventMask( everyEvent
) ;
627 UMAShowWatchCursor() ;
630 # if __option(profile)
631 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
636 // now avoid exceptions thrown for new (bad_alloc)
637 // FIXME CS for some changes outside wxMac does not compile anymore
639 std::__throws_bad_alloc
= 0 ;
644 s_macCursorRgn
= ::NewRgn() ;
646 // Mac OS X passes a process serial number command line argument when
647 // the application is launched from the Finder. This argument must be
648 // removed from the command line arguments before being handled by the
649 // application (otherwise applications would need to handle it)
652 static const wxChar
*ARG_PSN
= _T("-psn_");
653 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
655 // remove this argument
657 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
661 if ( !wxAppBase::Initialize(argc
, argv
) )
665 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
668 #if TARGET_API_MAC_OSX
669 // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
670 wxString startupCwd
= wxGetCwd() ;
671 if ( startupCwd
== wxT("/") || startupCwd
.Right(15) == wxT("/Contents/MacOS") )
673 CFURLRef url
= CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
674 CFURLRef urlParent
= CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault
, url
) ;
676 CFStringRef path
= CFURLCopyFileSystemPath ( urlParent
, kCFURLPOSIXPathStyle
) ;
677 CFRelease( urlParent
) ;
678 wxString cwd
= wxMacCFStringHolder(path
).AsString(wxLocale::GetSystemEncoding());
679 wxSetWorkingDirectory( cwd
) ;
683 wxMacCreateNotifierTable() ;
686 /* connect posted events to common-mode run loop so that wxPostEvent events
687 are handled even while we're in the menu or on a scrollbar */
688 CFRunLoopSourceContext event_posted_context
= {0};
689 event_posted_context
.perform
= macPostedEventCallback
;
690 m_macEventPosted
= CFRunLoopSourceCreate(NULL
,0,&event_posted_context
);
691 CFRunLoopAddSource(CFRunLoopGetCurrent(), m_macEventPosted
, kCFRunLoopCommonModes
);
694 UMAShowArrowCursor() ;
699 AEEventHandlerUPP sODocHandler
= NULL
;
700 AEEventHandlerUPP sOAppHandler
= NULL
;
701 AEEventHandlerUPP sPDocHandler
= NULL
;
702 AEEventHandlerUPP sRAppHandler
= NULL
;
703 AEEventHandlerUPP sQuitHandler
= NULL
;
705 bool wxApp::OnInitGui()
707 if( !wxAppBase::OnInitGui() )
710 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
714 InstallApplicationEventHandler(
715 GetwxMacAppEventHandlerUPP(),
716 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
721 sODocHandler
= NewAEEventHandlerUPP(AEHandleODoc
) ;
722 sOAppHandler
= NewAEEventHandlerUPP(AEHandleOApp
) ;
723 sPDocHandler
= NewAEEventHandlerUPP(AEHandlePDoc
) ;
724 sRAppHandler
= NewAEEventHandlerUPP(AEHandleRApp
) ;
725 sQuitHandler
= NewAEEventHandlerUPP(AEHandleQuit
) ;
727 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
728 sODocHandler
, 0 , FALSE
) ;
729 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
730 sOAppHandler
, 0 , FALSE
) ;
731 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
732 sPDocHandler
, 0 , FALSE
) ;
733 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
734 sRAppHandler
, 0 , FALSE
) ;
735 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
736 sQuitHandler
, 0 , FALSE
) ;
742 void wxApp::CleanUp()
745 wxToolTip::RemoveToolTips() ;
749 if (m_macEventPosted
)
751 CFRelease(m_macEventPosted
);
753 m_macEventPosted
= NULL
;
756 // One last chance for pending objects to be cleaned up
757 wxTheApp
->DeletePendingObjects();
759 wxMacDestroyNotifierTable() ;
762 # if __option(profile)
763 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
768 UMACleanupToolbox() ;
769 if (s_macCursorRgn
) {
770 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
775 RemoveEventHandler( (EventHandlerRef
)(wxTheApp
->m_macEventHandler
) );
780 AERemoveEventHandler( kCoreEventClass
, kAEOpenDocuments
,
781 sODocHandler
, FALSE
) ;
782 AERemoveEventHandler( kCoreEventClass
, kAEOpenApplication
,
783 sOAppHandler
, FALSE
) ;
784 AERemoveEventHandler( kCoreEventClass
, kAEPrintDocuments
,
785 sPDocHandler
, FALSE
) ;
786 AERemoveEventHandler( kCoreEventClass
, kAEReopenApplication
,
787 sRAppHandler
, FALSE
) ;
788 AERemoveEventHandler( kCoreEventClass
, kAEQuitApplication
,
789 sQuitHandler
, FALSE
) ;
791 DisposeAEEventHandlerUPP( sODocHandler
) ;
792 DisposeAEEventHandlerUPP( sOAppHandler
) ;
793 DisposeAEEventHandlerUPP( sPDocHandler
) ;
794 DisposeAEEventHandlerUPP( sRAppHandler
) ;
795 DisposeAEEventHandlerUPP( sQuitHandler
) ;
798 wxAppBase::CleanUp();
801 //----------------------------------------------------------------------
802 // misc initialization stuff
803 //----------------------------------------------------------------------
805 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
807 // for shared libraries we have to manually get the correct resource
808 // ref num upon initializing and releasing when terminating, therefore
809 // the __wxinitialize and __wxterminate must be used
812 void __sinit(void); /* (generated by linker) */
813 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
814 pascal void __terminate(void);
817 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
819 return __initialize( theInitBlock
) ;
822 pascal void __wxterminate(void)
827 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
829 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
831 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
832 OSStatus err
= noErr
;
835 switch( GetEventClass( event
) )
837 case kEventClassKeyboard
:
840 switch( GetEventKind(event
) )
842 case kEventRawKeyDown
:
843 rec
->what
= keyDown
;
845 case kEventRawKeyRepeat
:
846 rec
->what
= autoKey
;
848 case kEventRawKeyUp
:
851 case kEventRawKeyModifiersChanged
:
852 rec
->what
= nullEvent
;
861 unsigned char charCode
;
863 GetMouse( &rec
->where
) ;
865 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
866 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
867 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
868 rec
->modifiers
= modifiers
;
869 rec
->message
= (keyCode
<< 8 ) + charCode
;
873 case kEventClassTextInput
:
875 switch( GetEventKind( event
) )
877 case kEventTextInputUnicodeForKeyEvent
:
880 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
884 unsigned char charCode
;
886 GetMouse( &rec
->where
) ;
887 rec
->what
= keyDown
;
888 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
889 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
890 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
891 rec
->modifiers
= modifiers
;
892 rec
->message
= (keyCode
<< 8 ) + charCode
;
909 m_printMode
= wxPRINT_WINDOWS
;
911 m_macCurrentEvent
= NULL
;
912 m_macCurrentEventHandlerCallRef
= NULL
;
914 m_macEventPosted
= NULL
;
918 int wxApp::MainLoop()
922 RunApplicationEventLoop() ;
932 void wxApp::ExitMainLoop()
936 QuitApplicationEventLoop() ;
940 // Is a message/event pending?
941 bool wxApp::Pending()
943 // without the receive event (with pull param = false ) nothing is ever reported
945 ReceiveNextEvent (0, NULL
, kEventDurationNoWait
, false, &theEvent
);
946 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
949 // Dispatch a message.
950 bool wxApp::Dispatch()
957 void wxApp::OnIdle(wxIdleEvent
& event
)
959 wxAppBase::OnIdle(event
);
961 // If they are pending events, we must process them: pending events are
962 // either events to the threads other than main or events posted with
963 // wxPostEvent() functions
964 wxMacProcessNotifierAndPendingEvents();
966 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
967 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
970 void wxApp::WakeUpIdle()
973 if (m_macEventPosted
)
975 CFRunLoopSourceSignal(m_macEventPosted
);
987 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
990 GetTopWindow()->Close(TRUE
);
993 // Default behaviour: close the application with prompts. The
994 // user can veto the close, and therefore the end session.
995 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
999 if (!GetTopWindow()->Close(!event
.CanVeto()))
1004 extern "C" void wxCYield() ;
1010 // Yield to other processes
1012 bool wxApp::Yield(bool onlyIfNeeded
)
1016 if ( !onlyIfNeeded
)
1018 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1026 // by definition yield should handle all non-processed events
1030 OSStatus status
= noErr
;
1033 s_inReceiveEvent
= true ;
1034 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1035 s_inReceiveEvent
= false ;
1037 if ( status
== eventLoopTimedOutErr
)
1039 // make sure next time the event loop will trigger idle events
1040 sleepTime
= kEventDurationNoWait
;
1042 else if ( status
== eventLoopQuitErr
)
1044 // according to QA1061 this may also occur when a WakeUp Process
1049 MacHandleOneEvent( theEvent
) ;
1050 ReleaseEvent(theEvent
);
1052 } while( status
== noErr
) ;
1054 wxMacProcessNotifierAndPendingEvents() ;
1060 void wxApp::MacDoOneEvent()
1064 s_inReceiveEvent
= true ;
1065 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1066 s_inReceiveEvent
= false ;
1067 if ( status
== eventLoopTimedOutErr
)
1069 if ( wxTheApp
->ProcessIdle() )
1070 sleepTime
= kEventDurationNoWait
;
1072 sleepTime
= kEventDurationSecond
;
1074 else if ( status
== eventLoopQuitErr
)
1076 // according to QA1061 this may also occur when a WakeUp Process
1081 MacHandleOneEvent( theEvent
) ;
1082 ReleaseEvent(theEvent
);
1083 sleepTime
= kEventDurationNoWait
;
1087 DeletePendingObjects() ;
1088 wxMacProcessNotifierAndPendingEvents() ;
1091 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1093 // Override to process unhandled events as you please
1096 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1098 EventTargetRef theTarget
;
1099 theTarget
= GetEventDispatcherTarget();
1100 m_macCurrentEvent
= evr
;
1101 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1102 if(status
== eventNotHandledErr
)
1104 MacHandleUnhandledEvent(evr
);
1106 wxMacProcessNotifierAndPendingEvents() ;
1108 wxMutexGuiLeaveOrEnter();
1109 #endif // wxUSE_THREADS
1112 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1113 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1118 case kHomeCharCode
:
1121 case kEnterCharCode
:
1122 retval
= WXK_RETURN
;
1127 case kHelpCharCode
:
1130 case kBackspaceCharCode
:
1136 case kPageUpCharCode
:
1137 retval
= WXK_PAGEUP
;
1139 case kPageDownCharCode
:
1140 retval
= WXK_PAGEDOWN
;
1142 case kReturnCharCode
:
1143 retval
= WXK_RETURN
;
1145 case kFunctionKeyCharCode
:
1197 case kEscapeCharCode
:
1198 retval
= WXK_ESCAPE
;
1200 case kLeftArrowCharCode
:
1203 case kRightArrowCharCode
:
1204 retval
= WXK_RIGHT
;
1206 case kUpArrowCharCode
:
1209 case kDownArrowCharCode
:
1212 case kDeleteCharCode
:
1213 retval
= WXK_DELETE
;
1221 int wxMacKeyCodeToModifier(wxKeyCode key
)
1247 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1249 wxASSERT_MSG(key
!= WXK_LBUTTON
&& key
!= WXK_RBUTTON
&& key
!=
1250 WXK_MBUTTON
, wxT("can't use wxGetKeyState() for mouse buttons"));
1252 //if OS X > 10.2 (i.e. 10.2.x)
1253 //a known apple bug prevents the system from determining led
1254 //states with GetKeys... can only determine caps lock led
1255 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key
));
1257 // KeyMapByteArray keymap;
1258 // GetKeys((BigEndianLong*)keymap);
1259 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1264 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1271 keychar
= short(keymessage
& charCodeMask
);
1272 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1274 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1276 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1277 // and look at the character after
1279 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1280 keychar
= short(keyInfo
& charCodeMask
);
1282 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1283 long realkeyval
= keyval
;
1284 if ( keyval
== keychar
)
1286 // 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)
1287 realkeyval
= short(keymessage
& charCodeMask
) ;
1288 keyval
= wxToupper( keyval
) ;
1291 // Check for NUMPAD keys
1292 if (keyval
>= '0' && keyval
<= '9' && keycode
>= 82 && keycode
<= 92)
1294 keyval
= keyval
- '0' + WXK_NUMPAD0
;
1296 else if (keycode
>= 67 && keycode
<= 81)
1301 keyval
= WXK_NUMPAD_ENTER
;
1304 keyval
= WXK_NUMPAD_EQUAL
;
1307 keyval
= WXK_NUMPAD_MULTIPLY
;
1310 keyval
= WXK_NUMPAD_DIVIDE
;
1313 keyval
= WXK_NUMPAD_SUBTRACT
;
1316 keyval
= WXK_NUMPAD_ADD
;
1319 keyval
= WXK_NUMPAD_DECIMAL
;
1324 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1325 bool handled
= false ;
1326 event
.m_shiftDown
= modifiers
& shiftKey
;
1327 event
.m_controlDown
= modifiers
& controlKey
;
1328 event
.m_altDown
= modifiers
& optionKey
;
1329 event
.m_metaDown
= modifiers
& cmdKey
;
1330 event
.m_keyCode
= keyval
;
1332 event
.m_uniChar
= uniChar
;
1334 event
.m_rawCode
= keymessage
;
1335 event
.m_rawFlags
= modifiers
;
1338 event
.SetTimestamp(when
);
1339 event
.SetEventObject(focus
);
1340 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1341 if ( handled
&& event
.GetSkipped() )
1348 wxWindow
*ancestor
= focus
;
1351 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1354 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1355 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1358 if (ancestor
->IsTopLevel())
1360 ancestor
= ancestor
->GetParent();
1363 #endif // wxUSE_ACCEL
1367 wxTopLevelWindowMac
*tlw
= focus
->MacGetTopLevelWindow() ;
1371 event
.Skip( FALSE
) ;
1372 event
.SetEventType( wxEVT_CHAR_HOOK
);
1374 event
.m_keyCode
= realkeyval
;
1376 handled
= tlw
->GetEventHandler()->ProcessEvent( event
);
1377 if ( handled
&& event
.GetSkipped() )
1384 event
.Skip( FALSE
) ;
1385 event
.SetEventType( wxEVT_CHAR
) ;
1387 event
.m_keyCode
= realkeyval
;
1389 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1390 if ( handled
&& event
.GetSkipped() )
1393 if ( !handled
&& (keyval
== WXK_TAB
) )
1395 wxWindow
* iter
= focus
->GetParent() ;
1396 while( iter
&& !handled
)
1398 if ( iter
->HasFlag( wxTAB_TRAVERSAL
) )
1400 wxNavigationKeyEvent new_event
;
1401 new_event
.SetEventObject( focus
);
1402 new_event
.SetDirection( !event
.ShiftDown() );
1403 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1404 new_event
.SetWindowChange( event
.ControlDown() );
1405 new_event
.SetCurrentFocus( focus
);
1406 handled
= focus
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
1407 if ( handled
&& new_event
.GetSkipped() )
1410 iter
= iter
->GetParent() ;
1413 // backdoor handler for default return and command escape
1414 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1416 // if window is not having a focus still testing for default enter or cancel
1417 // TODO add the UMA version for ActiveNonFloatingWindow
1418 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1421 if ( keyval
== WXK_RETURN
)
1423 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1425 if ( def
&& def
->IsEnabled() )
1427 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1428 event
.SetEventObject(def
);
1429 def
->Command(event
);
1433 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1434 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1436 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1437 new_event
.SetEventObject( focus
);
1438 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1445 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1452 keychar
= short(keymessage
& charCodeMask
);
1453 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1454 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1456 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1457 // and look at the character after
1459 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1460 keychar
= short(keyInfo
& charCodeMask
);
1462 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1464 if ( keyval
== keychar
)
1466 keyval
= wxToupper( keyval
) ;
1469 // Check for NUMPAD keys
1470 if (keyval
>= '0' && keyval
<= '9' && keycode
>= 82 && keycode
<= 92)
1472 keyval
= keyval
- '0' + WXK_NUMPAD0
;
1474 else if (keycode
>= 67 && keycode
<= 81)
1479 keyval
= WXK_NUMPAD_ENTER
;
1482 keyval
= WXK_NUMPAD_EQUAL
;
1485 keyval
= WXK_NUMPAD_MULTIPLY
;
1488 keyval
= WXK_NUMPAD_DIVIDE
;
1491 keyval
= WXK_NUMPAD_SUBTRACT
;
1494 keyval
= WXK_NUMPAD_ADD
;
1497 keyval
= WXK_NUMPAD_DECIMAL
;
1502 bool handled
= false ;
1504 wxKeyEvent
event(wxEVT_KEY_UP
);
1505 event
.m_shiftDown
= modifiers
& shiftKey
;
1506 event
.m_controlDown
= modifiers
& controlKey
;
1507 event
.m_altDown
= modifiers
& optionKey
;
1508 event
.m_metaDown
= modifiers
& cmdKey
;
1509 event
.m_keyCode
= keyval
;
1511 event
.m_uniChar
= uniChar
;
1514 event
.m_rawCode
= keymessage
;
1515 event
.m_rawFlags
= modifiers
;
1518 event
.SetTimestamp(when
);
1519 event
.SetEventObject(focus
);
1520 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;