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
;
443 // for 'standard' commands
444 if ( id
== kHICommandPreferences
|| id
== kHICommandQuit
|| id
== kHICommandAbout
)
448 case kHICommandPreferences
:
449 id
= wxApp::s_macPreferencesMenuItemId
;
451 case kHICommandQuit
:
452 id
= wxApp::s_macExitMenuItemId
;
454 case kHICommandAbout
:
455 id
= wxApp::s_macAboutMenuItemId
;
459 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
462 wxMenu
* menu
= NULL
;
463 item
= mbar
->FindItem( id
, &menu
) ;
466 else if ( id
!= 0 && command
.menu
.menuRef
!= 0 && command
.menu
.menuItemIndex
!= 0 )
468 // make sure it is one of our own menus, or of the 'synthetic' apple and help menus , otherwise don't touch
469 MenuItemIndex firstUserHelpMenuItem
;
470 static MenuHandle mh
= NULL
;
473 if ( UMAGetHelpMenu( &mh
, &firstUserHelpMenuItem
) != noErr
)
479 // is it part of the application or the help menu, then look for the id directly
480 if ( ( GetMenuHandle( kwxMacAppleMenuId
) != NULL
&& command
.menu
.menuRef
== GetMenuHandle( kwxMacAppleMenuId
) ) ||
481 ( mh
!= NULL
&& command
.menu
.menuRef
== mh
) )
483 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
486 wxMenu
* menu
= NULL
;
487 item
= mbar
->FindItem( id
, &menu
) ;
492 wxMenu
* itsMenu
= NULL
;
494 GetMenuItemRefCon( command
.menu
.menuRef
, command
.menu
.menuItemIndex
, &refCon
) ;
495 itsMenu
= wxFindMenuFromMacMenu( command
.menu
.menuRef
) ;
496 if ( itsMenu
!= NULL
)
498 item
= (wxMenuItem
*) refCon
;
505 switch( cEvent
.GetKind() )
507 case kEventProcessCommand
:
509 if (item
->IsCheckable())
511 item
->Check( !item
->IsChecked() ) ;
514 item
->GetMenu()->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
518 case kEventCommandUpdateStatus
:
519 // eventually trigger an updateui round
529 static pascal OSStatus
wxMacAppApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
531 OSStatus result
= eventNotHandledErr
;
532 switch ( GetEventKind( event
) )
534 case kEventAppActivated
:
537 wxTheApp
->SetActive( true , NULL
) ;
541 case kEventAppDeactivated
:
544 wxTheApp
->SetActive( false , NULL
) ;
554 pascal OSStatus
wxMacAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
556 EventRef formerEvent
= (EventRef
) wxTheApp
->MacGetCurrentEvent() ;
557 EventHandlerCallRef formerEventHandlerCallRef
= (EventHandlerCallRef
) wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
558 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
560 OSStatus result
= eventNotHandledErr
;
561 switch( GetEventClass( event
) )
563 case kEventClassCommand
:
564 result
= wxMacAppCommandEventHandler( handler
, event
, data
) ;
566 case kEventClassApplication
:
567 result
= wxMacAppApplicationEventHandler( handler
, event
, data
) ;
569 case kEventClassMenu
:
570 result
= wxMacAppMenuEventHandler( handler
, event
, data
) ;
572 case kEventClassMouse
:
574 wxMacCarbonEvent
cEvent( event
) ;
577 Point screenMouseLocation
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ;
578 ::FindWindow(screenMouseLocation
, &window
);
579 // only send this event in case it had not already been sent to a tlw, as we get
580 // double events otherwise (in case event.skip) was called
581 if ( window
== NULL
)
582 result
= wxMacTopLevelMouseEventHandler( handler
, event
, NULL
) ;
585 case kEventClassAppleEvent
:
588 wxMacConvertEventToRecord( event
, &rec
) ;
589 result
= AEProcessAppleEvent( &rec
) ;
596 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerEventHandlerCallRef
) ;
601 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler
)
603 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
604 // we know it's there ;-)
605 WXIMPORT
char std::__throws_bad_alloc
;
610 pascal static void wxMacAssertOutputHandler(OSType componentSignature
, UInt32 options
,
611 const char *assertionString
, const char *exceptionLabelString
,
612 const char *errorString
, const char *fileName
, long lineNumber
, void *value
, ConstStr255Param outputMsg
)
614 // flow into assert handling
615 wxString fileNameStr
;
616 wxString assertionStr
;
617 wxString exceptionStr
;
620 fileNameStr
= wxString(fileName
, wxConvLocal
);
621 assertionStr
= wxString(assertionString
, wxConvLocal
);
622 exceptionStr
= wxString((exceptionLabelString
!=0) ? exceptionLabelString
: "", wxConvLocal
) ;
623 errorStr
= wxString((errorString
!=0) ? errorString
: "", wxConvLocal
) ;
625 fileNameStr
= fileName
;
626 assertionStr
= assertionString
;
627 exceptionStr
= (exceptionLabelString
!=0) ? exceptionLabelString
: "" ;
628 errorStr
= (errorString
!=0) ? errorString
: "" ;
633 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
634 assertionStr
.c_str() ,
635 exceptionStr
.c_str() ,
637 fileNameStr
.c_str(), lineNumber
,
641 wxOnAssert(fileNameStr
, lineNumber
, assertionStr
,
642 wxString::Format( wxT("%s %s value (%p)") ,exceptionStr
, errorStr
, value
) ) ;
650 /* m_macEventPosted run loop source callback: */
651 void macPostedEventCallback(void *unused
);
654 void macPostedEventCallback(void *unused
) {
655 wxTheApp
->ProcessPendingEvents(); }
658 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
663 InstallDebugAssertOutputHandler ( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler
) );
665 UMAInitToolbox( 4, sm_isEmbedded
) ;
666 SetEventMask( everyEvent
) ;
667 UMAShowWatchCursor() ;
670 # if __option(profile)
671 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
676 // now avoid exceptions thrown for new (bad_alloc)
677 // FIXME CS for some changes outside wxMac does not compile anymore
679 std::__throws_bad_alloc
= 0 ;
684 s_macCursorRgn
= ::NewRgn() ;
686 // Mac OS X passes a process serial number command line argument when
687 // the application is launched from the Finder. This argument must be
688 // removed from the command line arguments before being handled by the
689 // application (otherwise applications would need to handle it)
692 static const wxChar
*ARG_PSN
= _T("-psn_");
693 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
695 // remove this argument
697 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
701 if ( !wxAppBase::Initialize(argc
, argv
) )
705 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
708 #if TARGET_API_MAC_OSX
709 // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
710 wxString startupCwd
= wxGetCwd() ;
711 if ( startupCwd
== wxT("/") || startupCwd
.Right(15) == wxT("/Contents/MacOS") )
713 CFURLRef url
= CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
714 CFURLRef urlParent
= CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault
, url
) ;
716 CFStringRef path
= CFURLCopyFileSystemPath ( urlParent
, kCFURLPOSIXPathStyle
) ;
717 CFRelease( urlParent
) ;
718 wxString cwd
= wxMacCFStringHolder(path
).AsString(wxLocale::GetSystemEncoding());
719 wxSetWorkingDirectory( cwd
) ;
723 wxMacCreateNotifierTable() ;
726 /* connect posted events to common-mode run loop so that wxPostEvent events
727 are handled even while we're in the menu or on a scrollbar */
728 CFRunLoopSourceContext event_posted_context
= {0};
729 event_posted_context
.perform
= macPostedEventCallback
;
730 m_macEventPosted
= CFRunLoopSourceCreate(NULL
,0,&event_posted_context
);
731 CFRunLoopAddSource(CFRunLoopGetCurrent(), m_macEventPosted
, kCFRunLoopCommonModes
);
734 UMAShowArrowCursor() ;
739 AEEventHandlerUPP sODocHandler
= NULL
;
740 AEEventHandlerUPP sOAppHandler
= NULL
;
741 AEEventHandlerUPP sPDocHandler
= NULL
;
742 AEEventHandlerUPP sRAppHandler
= NULL
;
743 AEEventHandlerUPP sQuitHandler
= NULL
;
745 bool wxApp::OnInitGui()
747 if( !wxAppBase::OnInitGui() )
750 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
754 InstallApplicationEventHandler(
755 GetwxMacAppEventHandlerUPP(),
756 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
761 sODocHandler
= NewAEEventHandlerUPP(AEHandleODoc
) ;
762 sOAppHandler
= NewAEEventHandlerUPP(AEHandleOApp
) ;
763 sPDocHandler
= NewAEEventHandlerUPP(AEHandlePDoc
) ;
764 sRAppHandler
= NewAEEventHandlerUPP(AEHandleRApp
) ;
765 sQuitHandler
= NewAEEventHandlerUPP(AEHandleQuit
) ;
767 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
768 sODocHandler
, 0 , FALSE
) ;
769 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
770 sOAppHandler
, 0 , FALSE
) ;
771 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
772 sPDocHandler
, 0 , FALSE
) ;
773 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
774 sRAppHandler
, 0 , FALSE
) ;
775 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
776 sQuitHandler
, 0 , FALSE
) ;
782 void wxApp::CleanUp()
785 wxToolTip::RemoveToolTips() ;
789 if (m_macEventPosted
)
791 CFRelease(m_macEventPosted
);
793 m_macEventPosted
= NULL
;
796 // One last chance for pending objects to be cleaned up
797 wxTheApp
->DeletePendingObjects();
799 wxMacDestroyNotifierTable() ;
802 # if __option(profile)
803 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
808 UMACleanupToolbox() ;
809 if (s_macCursorRgn
) {
810 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
815 RemoveEventHandler( (EventHandlerRef
)(wxTheApp
->m_macEventHandler
) );
820 AERemoveEventHandler( kCoreEventClass
, kAEOpenDocuments
,
821 sODocHandler
, FALSE
) ;
822 AERemoveEventHandler( kCoreEventClass
, kAEOpenApplication
,
823 sOAppHandler
, FALSE
) ;
824 AERemoveEventHandler( kCoreEventClass
, kAEPrintDocuments
,
825 sPDocHandler
, FALSE
) ;
826 AERemoveEventHandler( kCoreEventClass
, kAEReopenApplication
,
827 sRAppHandler
, FALSE
) ;
828 AERemoveEventHandler( kCoreEventClass
, kAEQuitApplication
,
829 sQuitHandler
, FALSE
) ;
831 DisposeAEEventHandlerUPP( sODocHandler
) ;
832 DisposeAEEventHandlerUPP( sOAppHandler
) ;
833 DisposeAEEventHandlerUPP( sPDocHandler
) ;
834 DisposeAEEventHandlerUPP( sRAppHandler
) ;
835 DisposeAEEventHandlerUPP( sQuitHandler
) ;
838 wxAppBase::CleanUp();
841 //----------------------------------------------------------------------
842 // misc initialization stuff
843 //----------------------------------------------------------------------
845 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
847 // for shared libraries we have to manually get the correct resource
848 // ref num upon initializing and releasing when terminating, therefore
849 // the __wxinitialize and __wxterminate must be used
852 void __sinit(void); /* (generated by linker) */
853 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
854 pascal void __terminate(void);
857 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
859 return __initialize( theInitBlock
) ;
862 pascal void __wxterminate(void)
867 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
869 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
871 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
872 OSStatus err
= noErr
;
875 switch( GetEventClass( event
) )
877 case kEventClassKeyboard
:
880 switch( GetEventKind(event
) )
882 case kEventRawKeyDown
:
883 rec
->what
= keyDown
;
885 case kEventRawKeyRepeat
:
886 rec
->what
= autoKey
;
888 case kEventRawKeyUp
:
891 case kEventRawKeyModifiersChanged
:
892 rec
->what
= nullEvent
;
901 unsigned char charCode
;
903 GetMouse( &rec
->where
) ;
905 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
906 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
907 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
908 rec
->modifiers
= modifiers
;
909 rec
->message
= (keyCode
<< 8 ) + charCode
;
913 case kEventClassTextInput
:
915 switch( GetEventKind( event
) )
917 case kEventTextInputUnicodeForKeyEvent
:
920 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
924 unsigned char charCode
;
926 GetMouse( &rec
->where
) ;
927 rec
->what
= keyDown
;
928 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
929 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
930 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
931 rec
->modifiers
= modifiers
;
932 rec
->message
= (keyCode
<< 8 ) + charCode
;
949 m_printMode
= wxPRINT_WINDOWS
;
951 m_macCurrentEvent
= NULL
;
952 m_macCurrentEventHandlerCallRef
= NULL
;
954 m_macEventPosted
= NULL
;
958 int wxApp::MainLoop()
962 RunApplicationEventLoop() ;
972 void wxApp::ExitMainLoop()
976 QuitApplicationEventLoop() ;
980 // Is a message/event pending?
981 bool wxApp::Pending()
983 // without the receive event (with pull param = false ) nothing is ever reported
985 ReceiveNextEvent (0, NULL
, kEventDurationNoWait
, false, &theEvent
);
986 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
989 // Dispatch a message.
990 bool wxApp::Dispatch()
997 void wxApp::OnIdle(wxIdleEvent
& event
)
999 wxAppBase::OnIdle(event
);
1001 // If they are pending events, we must process them: pending events are
1002 // either events to the threads other than main or events posted with
1003 // wxPostEvent() functions
1004 wxMacProcessNotifierAndPendingEvents();
1006 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1007 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1010 void wxApp::WakeUpIdle()
1012 #ifdef __WXMAC_OSX__
1013 if (m_macEventPosted
)
1015 CFRunLoopSourceSignal(m_macEventPosted
);
1027 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1030 GetTopWindow()->Close(true);
1033 // Default behaviour: close the application with prompts. The
1034 // user can veto the close, and therefore the end session.
1035 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1039 if (!GetTopWindow()->Close(!event
.CanVeto()))
1044 extern "C" void wxCYield() ;
1050 // Yield to other processes
1052 bool wxApp::Yield(bool onlyIfNeeded
)
1056 if ( !onlyIfNeeded
)
1058 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1066 // by definition yield should handle all non-processed events
1070 OSStatus status
= noErr
;
1073 s_inReceiveEvent
= true ;
1074 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1075 s_inReceiveEvent
= false ;
1077 if ( status
== eventLoopTimedOutErr
)
1079 // make sure next time the event loop will trigger idle events
1080 sleepTime
= kEventDurationNoWait
;
1082 else if ( status
== eventLoopQuitErr
)
1084 // according to QA1061 this may also occur when a WakeUp Process
1089 MacHandleOneEvent( theEvent
) ;
1090 ReleaseEvent(theEvent
);
1092 } while( status
== noErr
) ;
1094 wxMacProcessNotifierAndPendingEvents() ;
1100 void wxApp::MacDoOneEvent()
1104 s_inReceiveEvent
= true ;
1105 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1106 s_inReceiveEvent
= false ;
1107 if ( status
== eventLoopTimedOutErr
)
1109 if ( wxTheApp
->ProcessIdle() )
1110 sleepTime
= kEventDurationNoWait
;
1112 sleepTime
= kEventDurationSecond
;
1114 else if ( status
== eventLoopQuitErr
)
1116 // according to QA1061 this may also occur when a WakeUp Process
1121 MacHandleOneEvent( theEvent
) ;
1122 ReleaseEvent(theEvent
);
1123 sleepTime
= kEventDurationNoWait
;
1127 DeletePendingObjects() ;
1128 wxMacProcessNotifierAndPendingEvents() ;
1131 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1133 // Override to process unhandled events as you please
1136 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1138 EventTargetRef theTarget
;
1139 theTarget
= GetEventDispatcherTarget();
1140 m_macCurrentEvent
= evr
;
1141 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1142 if(status
== eventNotHandledErr
)
1144 MacHandleUnhandledEvent(evr
);
1146 wxMacProcessNotifierAndPendingEvents() ;
1148 wxMutexGuiLeaveOrEnter();
1149 #endif // wxUSE_THREADS
1152 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1153 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1158 case kHomeCharCode
:
1162 case kEnterCharCode
:
1163 retval
= WXK_RETURN
;
1169 case kHelpCharCode
:
1173 case kBackspaceCharCode
:
1181 case kPageUpCharCode
:
1182 retval
= WXK_PAGEUP
;
1185 case kPageDownCharCode
:
1186 retval
= WXK_PAGEDOWN
;
1189 case kReturnCharCode
:
1190 retval
= WXK_RETURN
;
1193 case kFunctionKeyCharCode
:
1246 case kEscapeCharCode
:
1247 retval
= WXK_ESCAPE
;
1250 case kLeftArrowCharCode
:
1254 case kRightArrowCharCode
:
1255 retval
= WXK_RIGHT
;
1258 case kUpArrowCharCode
:
1262 case kDownArrowCharCode
:
1266 case kDeleteCharCode
:
1267 retval
= WXK_DELETE
;
1277 int wxMacKeyCodeToModifier(wxKeyCode key
)
1303 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1305 wxASSERT_MSG(key
!= WXK_LBUTTON
&& key
!= WXK_RBUTTON
&& key
!=
1306 WXK_MBUTTON
, wxT("can't use wxGetKeyState() for mouse buttons"));
1308 //if OS X > 10.2 (i.e. 10.2.x)
1309 //a known apple bug prevents the system from determining led
1310 //states with GetKeys... can only determine caps lock led
1311 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key
));
1313 // KeyMapByteArray keymap;
1314 // GetKeys((BigEndianLong*)keymap);
1315 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1320 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1327 keychar
= short(keymessage
& charCodeMask
);
1328 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1330 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1332 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1333 // and look at the character after
1335 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1336 keychar
= short(keyInfo
& charCodeMask
);
1338 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1339 long realkeyval
= keyval
;
1340 if ( keyval
== keychar
)
1342 // 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)
1343 realkeyval
= short(keymessage
& charCodeMask
) ;
1344 keyval
= wxToupper( keyval
) ;
1347 // Check for NUMPAD keys
1348 if (keyval
>= '0' && keyval
<= '9' && keycode
>= 82 && keycode
<= 92)
1350 keyval
= keyval
- '0' + WXK_NUMPAD0
;
1352 else if (keycode
>= 67 && keycode
<= 81)
1357 keyval
= WXK_NUMPAD_ENTER
;
1360 keyval
= WXK_NUMPAD_EQUAL
;
1363 keyval
= WXK_NUMPAD_MULTIPLY
;
1366 keyval
= WXK_NUMPAD_DIVIDE
;
1369 keyval
= WXK_NUMPAD_SUBTRACT
;
1372 keyval
= WXK_NUMPAD_ADD
;
1375 keyval
= WXK_NUMPAD_DECIMAL
;
1380 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1381 bool handled
= false ;
1382 event
.m_shiftDown
= modifiers
& shiftKey
;
1383 event
.m_controlDown
= modifiers
& controlKey
;
1384 event
.m_altDown
= modifiers
& optionKey
;
1385 event
.m_metaDown
= modifiers
& cmdKey
;
1386 event
.m_keyCode
= keyval
;
1388 event
.m_uniChar
= uniChar
;
1390 event
.m_rawCode
= keymessage
;
1391 event
.m_rawFlags
= modifiers
;
1394 event
.SetTimestamp(when
);
1395 event
.SetEventObject(focus
);
1396 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1397 if ( handled
&& event
.GetSkipped() )
1403 wxWindow
*ancestor
= focus
;
1406 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1409 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1410 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1413 if (ancestor
->IsTopLevel())
1415 ancestor
= ancestor
->GetParent();
1418 #endif // wxUSE_ACCEL
1422 wxTopLevelWindowMac
*tlw
= focus
->MacGetTopLevelWindow() ;
1426 event
.Skip( false ) ;
1427 event
.SetEventType( wxEVT_CHAR_HOOK
);
1429 event
.m_keyCode
= realkeyval
;
1431 handled
= tlw
->GetEventHandler()->ProcessEvent( event
);
1432 if ( handled
&& event
.GetSkipped() )
1439 event
.Skip( false ) ;
1440 event
.SetEventType( wxEVT_CHAR
) ;
1442 event
.m_keyCode
= realkeyval
;
1444 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1445 if ( handled
&& event
.GetSkipped() )
1448 if ( !handled
&& (keyval
== WXK_TAB
) )
1450 wxWindow
* iter
= focus
->GetParent() ;
1451 while( iter
&& !handled
)
1453 if ( iter
->HasFlag( wxTAB_TRAVERSAL
) )
1455 wxNavigationKeyEvent new_event
;
1456 new_event
.SetEventObject( focus
);
1457 new_event
.SetDirection( !event
.ShiftDown() );
1458 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1459 new_event
.SetWindowChange( event
.ControlDown() );
1460 new_event
.SetCurrentFocus( focus
);
1461 handled
= focus
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
1462 if ( handled
&& new_event
.GetSkipped() )
1465 iter
= iter
->GetParent() ;
1468 // backdoor handler for default return and command escape
1469 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1471 // if window is not having a focus still testing for default enter or cancel
1472 // TODO add the UMA version for ActiveNonFloatingWindow
1473 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1476 if ( keyval
== WXK_RETURN
)
1478 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1480 if ( def
&& def
->IsEnabled() )
1482 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1483 event
.SetEventObject(def
);
1484 def
->Command(event
);
1488 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1489 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1491 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1492 new_event
.SetEventObject( focus
);
1493 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1500 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1507 keychar
= short(keymessage
& charCodeMask
);
1508 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1509 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1511 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1512 // and look at the character after
1514 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1515 keychar
= short(keyInfo
& charCodeMask
);
1517 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1519 if ( keyval
== keychar
)
1521 keyval
= wxToupper( keyval
) ;
1524 // Check for NUMPAD keys
1525 if (keyval
>= '0' && keyval
<= '9' && keycode
>= 82 && keycode
<= 92)
1527 keyval
= keyval
- '0' + WXK_NUMPAD0
;
1529 else if (keycode
>= 67 && keycode
<= 81)
1534 keyval
= WXK_NUMPAD_ENTER
;
1537 keyval
= WXK_NUMPAD_EQUAL
;
1540 keyval
= WXK_NUMPAD_MULTIPLY
;
1543 keyval
= WXK_NUMPAD_DIVIDE
;
1546 keyval
= WXK_NUMPAD_SUBTRACT
;
1549 keyval
= WXK_NUMPAD_ADD
;
1552 keyval
= WXK_NUMPAD_DECIMAL
;
1557 bool handled
= false ;
1559 wxKeyEvent
event(wxEVT_KEY_UP
);
1560 event
.m_shiftDown
= modifiers
& shiftKey
;
1561 event
.m_controlDown
= modifiers
& controlKey
;
1562 event
.m_altDown
= modifiers
& optionKey
;
1563 event
.m_metaDown
= modifiers
& cmdKey
;
1564 event
.m_keyCode
= keyval
;
1566 event
.m_uniChar
= uniChar
;
1569 event
.m_rawCode
= keymessage
;
1570 event
.m_rawFlags
= modifiers
;
1573 event
.SetTimestamp(when
);
1574 event
.SetEventObject(focus
);
1575 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;