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 // Macintosh CommandID support - converting between native and wx IDs
355 //----------------------------------------------------------------------
357 // if no native match they just return the passed-in id
365 IdPair gCommandIds
[] =
367 { kHICommandCut
, wxID_CUT
} ,
368 { kHICommandCopy
, wxID_COPY
} ,
369 { kHICommandPaste
, wxID_PASTE
} ,
370 { kHICommandSelectAll
, wxID_SELECTALL
} ,
371 { kHICommandClear
, wxID_CLEAR
} ,
372 { kHICommandUndo
, wxID_UNDO
} ,
373 { kHICommandRedo
, wxID_REDO
} ,
376 int wxMacCommandToId( UInt32 macCommandId
)
380 if ( macCommandId
== kHICommandPreferences
|| macCommandId
== kHICommandQuit
|| macCommandId
== kHICommandAbout
)
382 switch ( macCommandId
)
384 case kHICommandPreferences
:
385 wxid
= wxApp::s_macPreferencesMenuItemId
;
387 case kHICommandQuit
:
388 wxid
= wxApp::s_macExitMenuItemId
;
390 case kHICommandAbout
:
391 wxid
= wxApp::s_macAboutMenuItemId
;
397 for ( size_t i
= 0 ; i
< WXSIZEOF(gCommandIds
) ; ++i
)
399 if ( gCommandIds
[i
].macId
== macCommandId
)
401 wxid
= gCommandIds
[i
].wxId
;
407 wxid
= (int) macCommandId
;
412 UInt32
wxIdToMacCommand( int wxId
)
416 if ( wxId
== wxApp::s_macPreferencesMenuItemId
)
417 macId
= kHICommandPreferences
;
418 else if (wxId
== wxApp::s_macExitMenuItemId
)
419 macId
= kHICommandQuit
;
420 else if (wxId
== wxApp::s_macAboutMenuItemId
)
421 macId
= kHICommandAbout
;
424 for ( size_t i
= 0 ; i
< WXSIZEOF(gCommandIds
) ; ++i
)
426 if ( gCommandIds
[i
].wxId
== wxId
)
428 macId
= gCommandIds
[i
].macId
;
439 wxMenu
* wxFindMenuFromMacCommand( const HICommand
&command
, wxMenuItem
* &item
)
441 wxMenu
* itemMenu
= NULL
;
444 // for 'standard' commands which don't have a wx-menu
445 if ( command
.commandID
== kHICommandPreferences
|| command
.commandID
== kHICommandQuit
|| command
.commandID
== kHICommandAbout
)
447 id
= wxMacCommandToId( command
.commandID
) ;
449 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
452 item
= mbar
->FindItem( id
, &itemMenu
) ;
455 else if ( command
.commandID
!= 0 && command
.menu
.menuRef
!= 0 && command
.menu
.menuItemIndex
!= 0 )
457 id
= wxMacCommandToId( command
.commandID
) ;
458 // make sure it is one of our own menus, or of the 'synthetic' apple and help menus , otherwise don't touch
459 MenuItemIndex firstUserHelpMenuItem
;
460 static MenuHandle mh
= NULL
;
463 if ( UMAGetHelpMenu( &mh
, &firstUserHelpMenuItem
) != noErr
)
469 // is it part of the application or the help menu, then look for the id directly
470 if ( ( GetMenuHandle( kwxMacAppleMenuId
) != NULL
&& command
.menu
.menuRef
== GetMenuHandle( kwxMacAppleMenuId
) ) ||
471 ( mh
!= NULL
&& command
.menu
.menuRef
== mh
) )
473 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
476 item
= mbar
->FindItem( id
, &itemMenu
) ;
482 GetMenuItemRefCon( command
.menu
.menuRef
, command
.menu
.menuItemIndex
, &refCon
) ;
483 itemMenu
= wxFindMenuFromMacMenu( command
.menu
.menuRef
) ;
484 if ( itemMenu
!= NULL
)
486 item
= (wxMenuItem
*) refCon
;
493 //----------------------------------------------------------------------
494 // Carbon Event Handler
495 //----------------------------------------------------------------------
497 static const EventTypeSpec eventList
[] =
499 { kEventClassCommand
, kEventProcessCommand
} ,
500 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
502 { kEventClassMenu
, kEventMenuOpening
},
503 { kEventClassMenu
, kEventMenuClosed
},
504 { kEventClassMenu
, kEventMenuTargetItem
},
506 { kEventClassApplication
, kEventAppActivated
} ,
507 { kEventClassApplication
, kEventAppDeactivated
} ,
508 // handling the quit event is not recommended by apple
509 // rather using the quit apple event - which we do
511 { kEventClassAppleEvent
, kEventAppleEvent
} ,
513 { kEventClassMouse
, kEventMouseDown
} ,
514 { kEventClassMouse
, kEventMouseMoved
} ,
515 { kEventClassMouse
, kEventMouseUp
} ,
516 { kEventClassMouse
, kEventMouseDragged
} ,
520 static pascal OSStatus
521 wxMacAppMenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
523 wxMacCarbonEvent
cEvent( event
) ;
524 MenuRef menuRef
= cEvent
.GetParameter
<MenuRef
>(kEventParamDirectObject
) ;
525 wxMenu
* menu
= wxFindMenuFromMacMenu( menuRef
) ;
531 switch (GetEventKind(event
))
533 case kEventMenuOpening
:
534 type
= wxEVT_MENU_OPEN
;
536 case kEventMenuClosed
:
537 type
= wxEVT_MENU_CLOSE
;
539 case kEventMenuTargetItem
:
540 cmd
= cEvent
.GetParameter
<MenuCommand
>(kEventParamMenuCommand
,typeMenuCommand
) ;
542 type
= wxEVT_MENU_HIGHLIGHT
;
545 wxFAIL_MSG(wxT("Unexpected menu event kind"));
551 wxMenuEvent
wxevent(type
, cmd
, menu
);
552 wxevent
.SetEventObject(menu
);
554 wxEvtHandler
* handler
= menu
->GetEventHandler();
555 if (handler
&& handler
->ProcessEvent(wxevent
))
561 wxWindow
*win
= menu
->GetInvokingWindow();
563 win
->GetEventHandler()->ProcessEvent(wxevent
);
568 return eventNotHandledErr
;
571 static pascal OSStatus
wxMacAppCommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
573 OSStatus result
= eventNotHandledErr
;
577 wxMacCarbonEvent
cEvent( event
) ;
578 cEvent
.GetParameter
<HICommand
>(kEventParamDirectObject
,typeHICommand
,&command
) ;
580 wxMenuItem
* item
= NULL
;
581 wxMenu
* itemMenu
= wxFindMenuFromMacCommand( command
, item
) ;
582 int id
= wxMacCommandToId( command
.commandID
) ;
586 wxASSERT( itemMenu
!= NULL
) ;
588 switch( cEvent
.GetKind() )
590 case kEventProcessCommand
:
592 if (item
->IsCheckable())
593 item
->Check( !item
->IsChecked() ) ;
595 if ( itemMenu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) )
599 case kEventCommandUpdateStatus
:
601 wxUpdateUIEvent
event(id
);
602 event
.SetEventObject( itemMenu
);
604 bool processed
= false;
606 // Try the menu's event handler
608 wxEvtHandler
*handler
= itemMenu
->GetEventHandler();
610 processed
= handler
->ProcessEvent(event
);
613 // Try the window the menu was popped up from (and up through the
617 const wxMenuBase
*menu
= itemMenu
;
620 wxWindow
*win
= menu
->GetInvokingWindow();
623 processed
= win
->GetEventHandler()->ProcessEvent(event
);
627 menu
= menu
->GetParent();
633 // if anything changed, update the changed attribute
634 if (event
.GetSetText())
635 itemMenu
->SetLabel(id
, event
.GetText());
636 if (event
.GetSetChecked())
637 itemMenu
->Check(id
, event
.GetChecked());
638 if (event
.GetSetEnabled())
639 itemMenu
->Enable(id
, event
.GetEnabled());
652 static pascal OSStatus
wxMacAppApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
654 OSStatus result
= eventNotHandledErr
;
655 switch ( GetEventKind( event
) )
657 case kEventAppActivated
:
660 wxTheApp
->SetActive( true , NULL
) ;
664 case kEventAppDeactivated
:
667 wxTheApp
->SetActive( false , NULL
) ;
677 pascal OSStatus
wxMacAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
679 EventRef formerEvent
= (EventRef
) wxTheApp
->MacGetCurrentEvent() ;
680 EventHandlerCallRef formerEventHandlerCallRef
= (EventHandlerCallRef
) wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
681 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
683 OSStatus result
= eventNotHandledErr
;
684 switch( GetEventClass( event
) )
686 case kEventClassCommand
:
687 result
= wxMacAppCommandEventHandler( handler
, event
, data
) ;
689 case kEventClassApplication
:
690 result
= wxMacAppApplicationEventHandler( handler
, event
, data
) ;
692 case kEventClassMenu
:
693 result
= wxMacAppMenuEventHandler( handler
, event
, data
) ;
695 case kEventClassMouse
:
697 wxMacCarbonEvent
cEvent( event
) ;
700 Point screenMouseLocation
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ;
701 ::FindWindow(screenMouseLocation
, &window
);
702 // only send this event in case it had not already been sent to a tlw, as we get
703 // double events otherwise (in case event.skip) was called
704 if ( window
== NULL
)
705 result
= wxMacTopLevelMouseEventHandler( handler
, event
, NULL
) ;
708 case kEventClassAppleEvent
:
711 wxMacConvertEventToRecord( event
, &rec
) ;
712 result
= AEProcessAppleEvent( &rec
) ;
719 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerEventHandlerCallRef
) ;
724 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler
)
726 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
727 // we know it's there ;-)
728 WXIMPORT
char std::__throws_bad_alloc
;
733 pascal static void wxMacAssertOutputHandler(OSType componentSignature
, UInt32 options
,
734 const char *assertionString
, const char *exceptionLabelString
,
735 const char *errorString
, const char *fileName
, long lineNumber
, void *value
, ConstStr255Param outputMsg
)
737 // flow into assert handling
738 wxString fileNameStr
;
739 wxString assertionStr
;
740 wxString exceptionStr
;
743 fileNameStr
= wxString(fileName
, wxConvLocal
);
744 assertionStr
= wxString(assertionString
, wxConvLocal
);
745 exceptionStr
= wxString((exceptionLabelString
!=0) ? exceptionLabelString
: "", wxConvLocal
) ;
746 errorStr
= wxString((errorString
!=0) ? errorString
: "", wxConvLocal
) ;
748 fileNameStr
= fileName
;
749 assertionStr
= assertionString
;
750 exceptionStr
= (exceptionLabelString
!=0) ? exceptionLabelString
: "" ;
751 errorStr
= (errorString
!=0) ? errorString
: "" ;
756 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
757 assertionStr
.c_str() ,
758 exceptionStr
.c_str() ,
760 fileNameStr
.c_str(), lineNumber
,
764 wxOnAssert(fileNameStr
, lineNumber
, assertionStr
,
765 wxString::Format( wxT("%s %s value (%p)") ,exceptionStr
, errorStr
, value
) ) ;
773 /* m_macEventPosted run loop source callback: */
774 void macPostedEventCallback(void *unused
);
777 void macPostedEventCallback(void *unused
) {
778 wxTheApp
->ProcessPendingEvents(); }
781 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
786 InstallDebugAssertOutputHandler ( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler
) );
788 UMAInitToolbox( 4, sm_isEmbedded
) ;
789 SetEventMask( everyEvent
) ;
790 UMAShowWatchCursor() ;
793 # if __option(profile)
794 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
799 // now avoid exceptions thrown for new (bad_alloc)
800 // FIXME CS for some changes outside wxMac does not compile anymore
802 std::__throws_bad_alloc
= 0 ;
807 s_macCursorRgn
= ::NewRgn() ;
809 // Mac OS X passes a process serial number command line argument when
810 // the application is launched from the Finder. This argument must be
811 // removed from the command line arguments before being handled by the
812 // application (otherwise applications would need to handle it)
815 static const wxChar
*ARG_PSN
= _T("-psn_");
816 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
818 // remove this argument
820 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
824 if ( !wxAppBase::Initialize(argc
, argv
) )
828 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
831 #if TARGET_API_MAC_OSX
832 // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
833 wxString startupCwd
= wxGetCwd() ;
834 if ( startupCwd
== wxT("/") || startupCwd
.Right(15) == wxT("/Contents/MacOS") )
836 CFURLRef url
= CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
837 CFURLRef urlParent
= CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault
, url
) ;
839 CFStringRef path
= CFURLCopyFileSystemPath ( urlParent
, kCFURLPOSIXPathStyle
) ;
840 CFRelease( urlParent
) ;
841 wxString cwd
= wxMacCFStringHolder(path
).AsString(wxLocale::GetSystemEncoding());
842 wxSetWorkingDirectory( cwd
) ;
846 wxMacCreateNotifierTable() ;
849 /* connect posted events to common-mode run loop so that wxPostEvent events
850 are handled even while we're in the menu or on a scrollbar */
851 CFRunLoopSourceContext event_posted_context
= {0};
852 event_posted_context
.perform
= macPostedEventCallback
;
853 m_macEventPosted
= CFRunLoopSourceCreate(NULL
,0,&event_posted_context
);
854 CFRunLoopAddSource(CFRunLoopGetCurrent(), m_macEventPosted
, kCFRunLoopCommonModes
);
857 UMAShowArrowCursor() ;
862 AEEventHandlerUPP sODocHandler
= NULL
;
863 AEEventHandlerUPP sOAppHandler
= NULL
;
864 AEEventHandlerUPP sPDocHandler
= NULL
;
865 AEEventHandlerUPP sRAppHandler
= NULL
;
866 AEEventHandlerUPP sQuitHandler
= NULL
;
868 bool wxApp::OnInitGui()
870 if( !wxAppBase::OnInitGui() )
873 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
877 InstallApplicationEventHandler(
878 GetwxMacAppEventHandlerUPP(),
879 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
884 sODocHandler
= NewAEEventHandlerUPP(AEHandleODoc
) ;
885 sOAppHandler
= NewAEEventHandlerUPP(AEHandleOApp
) ;
886 sPDocHandler
= NewAEEventHandlerUPP(AEHandlePDoc
) ;
887 sRAppHandler
= NewAEEventHandlerUPP(AEHandleRApp
) ;
888 sQuitHandler
= NewAEEventHandlerUPP(AEHandleQuit
) ;
890 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
891 sODocHandler
, 0 , FALSE
) ;
892 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
893 sOAppHandler
, 0 , FALSE
) ;
894 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
895 sPDocHandler
, 0 , FALSE
) ;
896 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
897 sRAppHandler
, 0 , FALSE
) ;
898 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
899 sQuitHandler
, 0 , FALSE
) ;
905 void wxApp::CleanUp()
908 wxToolTip::RemoveToolTips() ;
912 if (m_macEventPosted
)
914 CFRelease(m_macEventPosted
);
916 m_macEventPosted
= NULL
;
919 // One last chance for pending objects to be cleaned up
920 wxTheApp
->DeletePendingObjects();
922 wxMacDestroyNotifierTable() ;
925 # if __option(profile)
926 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
931 UMACleanupToolbox() ;
932 if (s_macCursorRgn
) {
933 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
938 RemoveEventHandler( (EventHandlerRef
)(wxTheApp
->m_macEventHandler
) );
943 AERemoveEventHandler( kCoreEventClass
, kAEOpenDocuments
,
944 sODocHandler
, FALSE
) ;
945 AERemoveEventHandler( kCoreEventClass
, kAEOpenApplication
,
946 sOAppHandler
, FALSE
) ;
947 AERemoveEventHandler( kCoreEventClass
, kAEPrintDocuments
,
948 sPDocHandler
, FALSE
) ;
949 AERemoveEventHandler( kCoreEventClass
, kAEReopenApplication
,
950 sRAppHandler
, FALSE
) ;
951 AERemoveEventHandler( kCoreEventClass
, kAEQuitApplication
,
952 sQuitHandler
, FALSE
) ;
954 DisposeAEEventHandlerUPP( sODocHandler
) ;
955 DisposeAEEventHandlerUPP( sOAppHandler
) ;
956 DisposeAEEventHandlerUPP( sPDocHandler
) ;
957 DisposeAEEventHandlerUPP( sRAppHandler
) ;
958 DisposeAEEventHandlerUPP( sQuitHandler
) ;
961 wxAppBase::CleanUp();
964 //----------------------------------------------------------------------
965 // misc initialization stuff
966 //----------------------------------------------------------------------
968 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
970 // for shared libraries we have to manually get the correct resource
971 // ref num upon initializing and releasing when terminating, therefore
972 // the __wxinitialize and __wxterminate must be used
975 void __sinit(void); /* (generated by linker) */
976 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
977 pascal void __terminate(void);
980 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
982 return __initialize( theInitBlock
) ;
985 pascal void __wxterminate(void)
990 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
992 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
994 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
995 OSStatus err
= noErr
;
998 switch( GetEventClass( event
) )
1000 case kEventClassKeyboard
:
1003 switch( GetEventKind(event
) )
1005 case kEventRawKeyDown
:
1006 rec
->what
= keyDown
;
1008 case kEventRawKeyRepeat
:
1009 rec
->what
= autoKey
;
1011 case kEventRawKeyUp
:
1014 case kEventRawKeyModifiersChanged
:
1015 rec
->what
= nullEvent
;
1024 unsigned char charCode
;
1026 GetMouse( &rec
->where
) ;
1028 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1029 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1030 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1031 rec
->modifiers
= modifiers
;
1032 rec
->message
= (keyCode
<< 8 ) + charCode
;
1036 case kEventClassTextInput
:
1038 switch( GetEventKind( event
) )
1040 case kEventTextInputUnicodeForKeyEvent
:
1043 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
1047 unsigned char charCode
;
1049 GetMouse( &rec
->where
) ;
1050 rec
->what
= keyDown
;
1051 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1052 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1053 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1054 rec
->modifiers
= modifiers
;
1055 rec
->message
= (keyCode
<< 8 ) + charCode
;
1072 m_printMode
= wxPRINT_WINDOWS
;
1074 m_macCurrentEvent
= NULL
;
1075 m_macCurrentEventHandlerCallRef
= NULL
;
1076 #ifdef __WXMAC_OSX__
1077 m_macEventPosted
= NULL
;
1081 int wxApp::MainLoop()
1085 RunApplicationEventLoop() ;
1095 void wxApp::ExitMainLoop()
1097 m_keepGoing
= false;
1099 QuitApplicationEventLoop() ;
1103 // Is a message/event pending?
1104 bool wxApp::Pending()
1106 // without the receive event (with pull param = false ) nothing is ever reported
1108 ReceiveNextEvent (0, NULL
, kEventDurationNoWait
, false, &theEvent
);
1109 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1112 // Dispatch a message.
1113 bool wxApp::Dispatch()
1120 void wxApp::OnIdle(wxIdleEvent
& event
)
1122 wxAppBase::OnIdle(event
);
1124 // If they are pending events, we must process them: pending events are
1125 // either events to the threads other than main or events posted with
1126 // wxPostEvent() functions
1127 wxMacProcessNotifierAndPendingEvents();
1129 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1130 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1133 void wxApp::WakeUpIdle()
1135 #ifdef __WXMAC_OSX__
1136 if (m_macEventPosted
)
1138 CFRunLoopSourceSignal(m_macEventPosted
);
1150 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1153 GetTopWindow()->Close(true);
1156 // Default behaviour: close the application with prompts. The
1157 // user can veto the close, and therefore the end session.
1158 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1162 if (!GetTopWindow()->Close(!event
.CanVeto()))
1167 extern "C" void wxCYield() ;
1173 // Yield to other processes
1175 bool wxApp::Yield(bool onlyIfNeeded
)
1179 if ( !onlyIfNeeded
)
1181 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1189 // by definition yield should handle all non-processed events
1193 OSStatus status
= noErr
;
1196 s_inReceiveEvent
= true ;
1197 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1198 s_inReceiveEvent
= false ;
1200 if ( status
== eventLoopTimedOutErr
)
1202 // make sure next time the event loop will trigger idle events
1203 sleepTime
= kEventDurationNoWait
;
1205 else if ( status
== eventLoopQuitErr
)
1207 // according to QA1061 this may also occur when a WakeUp Process
1212 MacHandleOneEvent( theEvent
) ;
1213 ReleaseEvent(theEvent
);
1215 } while( status
== noErr
) ;
1217 wxMacProcessNotifierAndPendingEvents() ;
1223 void wxApp::MacDoOneEvent()
1227 s_inReceiveEvent
= true ;
1228 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1229 s_inReceiveEvent
= false ;
1230 if ( status
== eventLoopTimedOutErr
)
1232 if ( wxTheApp
->ProcessIdle() )
1233 sleepTime
= kEventDurationNoWait
;
1235 sleepTime
= kEventDurationSecond
;
1237 else if ( status
== eventLoopQuitErr
)
1239 // according to QA1061 this may also occur when a WakeUp Process
1244 MacHandleOneEvent( theEvent
) ;
1245 ReleaseEvent(theEvent
);
1246 sleepTime
= kEventDurationNoWait
;
1250 DeletePendingObjects() ;
1251 wxMacProcessNotifierAndPendingEvents() ;
1254 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1256 // Override to process unhandled events as you please
1259 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1261 EventTargetRef theTarget
;
1262 theTarget
= GetEventDispatcherTarget();
1263 m_macCurrentEvent
= evr
;
1264 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1265 if(status
== eventNotHandledErr
)
1267 MacHandleUnhandledEvent(evr
);
1269 wxMacProcessNotifierAndPendingEvents() ;
1271 wxMutexGuiLeaveOrEnter();
1272 #endif // wxUSE_THREADS
1275 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1276 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1281 case kHomeCharCode
:
1285 case kEnterCharCode
:
1286 retval
= WXK_RETURN
;
1292 case kHelpCharCode
:
1296 case kBackspaceCharCode
:
1304 case kPageUpCharCode
:
1305 retval
= WXK_PAGEUP
;
1308 case kPageDownCharCode
:
1309 retval
= WXK_PAGEDOWN
;
1312 case kReturnCharCode
:
1313 retval
= WXK_RETURN
;
1316 case kFunctionKeyCharCode
:
1369 case kEscapeCharCode
:
1370 retval
= WXK_ESCAPE
;
1373 case kLeftArrowCharCode
:
1377 case kRightArrowCharCode
:
1378 retval
= WXK_RIGHT
;
1381 case kUpArrowCharCode
:
1385 case kDownArrowCharCode
:
1389 case kDeleteCharCode
:
1390 retval
= WXK_DELETE
;
1400 int wxMacKeyCodeToModifier(wxKeyCode key
)
1426 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1428 wxASSERT_MSG(key
!= WXK_LBUTTON
&& key
!= WXK_RBUTTON
&& key
!=
1429 WXK_MBUTTON
, wxT("can't use wxGetKeyState() for mouse buttons"));
1431 //if OS X > 10.2 (i.e. 10.2.x)
1432 //a known apple bug prevents the system from determining led
1433 //states with GetKeys... can only determine caps lock led
1434 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key
));
1436 // KeyMapByteArray keymap;
1437 // GetKeys((BigEndianLong*)keymap);
1438 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1443 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1450 keychar
= short(keymessage
& charCodeMask
);
1451 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1453 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1455 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1456 // and look at the character after
1458 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1459 keychar
= short(keyInfo
& charCodeMask
);
1461 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1462 long realkeyval
= keyval
;
1463 if ( keyval
== keychar
)
1465 // 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)
1466 realkeyval
= short(keymessage
& charCodeMask
) ;
1467 keyval
= wxToupper( keyval
) ;
1470 // Check for NUMPAD keys
1471 if (keyval
>= '0' && keyval
<= '9' && keycode
>= 82 && keycode
<= 92)
1473 keyval
= keyval
- '0' + WXK_NUMPAD0
;
1475 else if (keycode
>= 67 && keycode
<= 81)
1480 keyval
= WXK_NUMPAD_ENTER
;
1483 keyval
= WXK_NUMPAD_EQUAL
;
1486 keyval
= WXK_NUMPAD_MULTIPLY
;
1489 keyval
= WXK_NUMPAD_DIVIDE
;
1492 keyval
= WXK_NUMPAD_SUBTRACT
;
1495 keyval
= WXK_NUMPAD_ADD
;
1498 keyval
= WXK_NUMPAD_DECIMAL
;
1503 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1504 bool handled
= false ;
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
;
1513 event
.m_rawCode
= keymessage
;
1514 event
.m_rawFlags
= modifiers
;
1517 event
.SetTimestamp(when
);
1518 event
.SetEventObject(focus
);
1519 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1520 if ( handled
&& event
.GetSkipped() )
1526 wxWindow
*ancestor
= focus
;
1529 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1532 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1533 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1536 if (ancestor
->IsTopLevel())
1538 ancestor
= ancestor
->GetParent();
1541 #endif // wxUSE_ACCEL
1545 wxTopLevelWindowMac
*tlw
= focus
->MacGetTopLevelWindow() ;
1549 event
.Skip( false ) ;
1550 event
.SetEventType( wxEVT_CHAR_HOOK
);
1552 event
.m_keyCode
= realkeyval
;
1554 handled
= tlw
->GetEventHandler()->ProcessEvent( event
);
1555 if ( handled
&& event
.GetSkipped() )
1562 event
.Skip( false ) ;
1563 event
.SetEventType( wxEVT_CHAR
) ;
1565 event
.m_keyCode
= realkeyval
;
1567 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1568 if ( handled
&& event
.GetSkipped() )
1571 if ( !handled
&& (keyval
== WXK_TAB
) )
1573 wxWindow
* iter
= focus
->GetParent() ;
1574 while( iter
&& !handled
)
1576 if ( iter
->HasFlag( wxTAB_TRAVERSAL
) )
1578 wxNavigationKeyEvent new_event
;
1579 new_event
.SetEventObject( focus
);
1580 new_event
.SetDirection( !event
.ShiftDown() );
1581 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1582 new_event
.SetWindowChange( event
.ControlDown() );
1583 new_event
.SetCurrentFocus( focus
);
1584 handled
= focus
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
1585 if ( handled
&& new_event
.GetSkipped() )
1588 iter
= iter
->GetParent() ;
1591 // backdoor handler for default return and command escape
1592 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1594 // if window is not having a focus still testing for default enter or cancel
1595 // TODO add the UMA version for ActiveNonFloatingWindow
1596 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1599 if ( keyval
== WXK_RETURN
)
1601 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1603 if ( def
&& def
->IsEnabled() )
1605 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1606 event
.SetEventObject(def
);
1607 def
->Command(event
);
1611 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1612 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1614 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1615 new_event
.SetEventObject( focus
);
1616 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1623 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1630 keychar
= short(keymessage
& charCodeMask
);
1631 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1632 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1634 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1635 // and look at the character after
1637 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1638 keychar
= short(keyInfo
& charCodeMask
);
1640 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1642 if ( keyval
== keychar
)
1644 keyval
= wxToupper( keyval
) ;
1647 // Check for NUMPAD keys
1648 if (keyval
>= '0' && keyval
<= '9' && keycode
>= 82 && keycode
<= 92)
1650 keyval
= keyval
- '0' + WXK_NUMPAD0
;
1652 else if (keycode
>= 67 && keycode
<= 81)
1657 keyval
= WXK_NUMPAD_ENTER
;
1660 keyval
= WXK_NUMPAD_EQUAL
;
1663 keyval
= WXK_NUMPAD_MULTIPLY
;
1666 keyval
= WXK_NUMPAD_DIVIDE
;
1669 keyval
= WXK_NUMPAD_SUBTRACT
;
1672 keyval
= WXK_NUMPAD_ADD
;
1675 keyval
= WXK_NUMPAD_DECIMAL
;
1680 bool handled
= false ;
1682 wxKeyEvent
event(wxEVT_KEY_UP
);
1683 event
.m_shiftDown
= modifiers
& shiftKey
;
1684 event
.m_controlDown
= modifiers
& controlKey
;
1685 event
.m_altDown
= modifiers
& optionKey
;
1686 event
.m_metaDown
= modifiers
& cmdKey
;
1687 event
.m_keyCode
= keyval
;
1689 event
.m_uniChar
= uniChar
;
1692 event
.m_rawCode
= keymessage
;
1693 event
.m_rawFlags
= modifiers
;
1696 event
.SetTimestamp(when
);
1697 event
.SetEventObject(focus
);
1698 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;