1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/app.cpp
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
20 #include "wx/window.h"
23 #include "wx/button.h"
27 #include "wx/palette.h"
29 #include "wx/cursor.h"
30 #include "wx/dialog.h"
31 #include "wx/msgdlg.h"
32 #include "wx/textctrl.h"
33 #include "wx/memory.h"
34 #include "wx/gdicmn.h"
35 #include "wx/module.h"
38 #include "wx/tooltip.h"
39 #include "wx/docview.h"
40 #include "wx/filename.h"
47 #include "wx/mac/uma.h"
50 # include <CoreServices/CoreServices.h>
51 # if defined(WXMAKINGDLL_CORE)
52 # include <mach-o/dyld.h>
56 // Keep linker from discarding wxStockGDIMac
57 wxFORCE_LINK_MODULE(gdiobj
)
59 // statics for implementation
60 static bool s_inYield
= false;
61 static bool s_inReceiveEvent
= false ;
62 static EventTime sleepTime
= kEventDurationNoWait
;
65 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
66 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
67 EVT_IDLE(wxApp::OnIdle
)
68 EVT_END_SESSION(wxApp::OnEndSession
)
69 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
73 // platform specific static variables
74 static const short kwxMacAppleMenuId
= 1 ;
76 wxWindow
* wxApp::s_captureWindow
= NULL
;
77 long wxApp::s_lastModifiers
= 0 ;
79 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
80 long wxApp::s_macPreferencesMenuItemId
= wxID_PREFERENCES
;
81 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
82 wxString
wxApp::s_macHelpMenuTitleName
= wxT("&Help") ;
84 bool wxApp::sm_isEmbedded
= false; // Normally we're not a plugin
86 //----------------------------------------------------------------------
87 // Core Apple Event Support
88 //----------------------------------------------------------------------
90 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon refcon
) ;
91 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon refcon
) ;
92 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon refcon
) ;
93 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon refcon
) ;
94 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon refcon
) ;
96 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon
WXUNUSED(refcon
) )
98 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
101 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon
WXUNUSED(refcon
) )
103 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
106 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon
WXUNUSED(refcon
) )
108 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
111 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon
WXUNUSED(refcon
) )
113 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
116 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon
WXUNUSED(refcon
) )
118 return wxTheApp
->MacHandleAERApp( (AppleEvent
*) event
, reply
) ;
121 pascal OSErr
AEHandleGURL( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon
WXUNUSED(refcon
) )
123 return wxTheApp
->MacHandleAEGURL((WXEVENTREF
*)event
, reply
) ;
127 // AEODoc Calls MacOpenFile on each of the files passed
129 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
133 DescType returnedType
;
139 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
143 err
= AECountItems(&docList
, &itemsInList
);
147 ProcessSerialNumber PSN
;
148 PSN
.highLongOfPSN
= 0 ;
149 PSN
.lowLongOfPSN
= kCurrentProcess
;
150 SetFrontProcess( &PSN
) ;
155 for (i
= 1; i
<= itemsInList
; i
++)
158 &docList
, i
, typeFSRef
, &keywd
, &returnedType
,
159 (Ptr
)&theRef
, sizeof(theRef
), &actualSize
);
160 fName
= wxMacFSRefToPath( &theRef
) ;
168 // AEODoc Calls MacOpenURL on the url passed
170 short wxApp::MacHandleAEGURL(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
172 DescType returnedType
;
175 OSErr err
= AEGetParamPtr((AppleEvent
*)event
, keyDirectObject
, typeChar
,
176 &returnedType
, url
, sizeof(url
)-1,
181 url
[actualSize
] = '\0'; // Terminate the C string
183 ProcessSerialNumber PSN
;
184 PSN
.highLongOfPSN
= 0 ;
185 PSN
.lowLongOfPSN
= kCurrentProcess
;
186 SetFrontProcess( &PSN
) ;
188 MacOpenURL(wxString(url
, wxConvUTF8
));
193 // AEPDoc Calls MacPrintFile on each of the files passed
195 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
199 DescType returnedType
;
205 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
209 err
= AECountItems(&docList
, &itemsInList
);
213 ProcessSerialNumber PSN
;
214 PSN
.highLongOfPSN
= 0 ;
215 PSN
.lowLongOfPSN
= kCurrentProcess
;
216 SetFrontProcess( &PSN
) ;
221 for (i
= 1; i
<= itemsInList
; i
++)
224 &docList
, i
, typeFSRef
, &keywd
, &returnedType
,
225 (Ptr
)&theRef
, sizeof(theRef
), &actualSize
);
226 fName
= wxMacFSRefToPath( &theRef
) ;
234 // AEOApp calls MacNewFile
236 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
242 // AEQuit attempts to quit the application
244 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
246 wxWindow
* win
= GetTopWindow() ;
249 wxCommandEvent
exitEvent(wxEVT_COMMAND_MENU_SELECTED
, s_macExitMenuItemId
);
250 if (!win
->ProcessEvent(exitEvent
))
261 // AEROApp calls MacReopenApp
263 short wxApp::MacHandleAERApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
270 //----------------------------------------------------------------------
271 // Support Routines linking the Mac...File Calls to the Document Manager
272 //----------------------------------------------------------------------
274 void wxApp::MacOpenFile(const wxString
& fileName
)
276 #if wxUSE_DOC_VIEW_ARCHITECTURE
277 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
279 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
283 void wxApp::MacOpenURL(const wxString
& WXUNUSED(url
) )
287 void wxApp::MacPrintFile(const wxString
& fileName
)
289 #if wxUSE_DOC_VIEW_ARCHITECTURE
291 #if wxUSE_PRINTING_ARCHITECTURE
292 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
295 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
298 wxView
* view
= doc
->GetFirstView() ;
301 wxPrintout
*printout
= view
->OnCreatePrintout();
305 printer
.Print(view
->GetFrame(), printout
, true);
312 doc
->DeleteAllViews();
313 dm
->RemoveDocument(doc
) ;
324 void wxApp::MacNewFile()
328 void wxApp::MacReopenApp()
331 // if there is no open window -> create a new one
332 // if all windows are hidden -> show the first
333 // if some windows are not hidden -> do nothing
335 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
342 wxTopLevelWindow
* firstIconized
= NULL
;
343 wxTopLevelWindow
* firstHidden
= NULL
;
346 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->GetData();
347 if ( !win
->IsShown() )
349 // make sure we don't show 'virtual toplevel windows' like wxTaskBarIconWindow
350 if ( firstHidden
== NULL
&& ( wxDynamicCast( win
, wxFrame
) || wxDynamicCast( win
, wxDialog
) ) )
353 else if ( win
->IsIconized() )
355 if ( firstIconized
== NULL
)
356 firstIconized
= win
;
360 // we do have a visible, non-iconized toplevelwindow -> do nothing
364 node
= node
->GetNext();
368 firstIconized
->Iconize( false ) ;
369 else if ( firstHidden
)
370 firstHidden
->Show( true );
374 //----------------------------------------------------------------------
375 // Macintosh CommandID support - converting between native and wx IDs
376 //----------------------------------------------------------------------
378 // if no native match they just return the passed-in id
386 IdPair gCommandIds
[] =
388 { kHICommandCut
, wxID_CUT
} ,
389 { kHICommandCopy
, wxID_COPY
} ,
390 { kHICommandPaste
, wxID_PASTE
} ,
391 { kHICommandSelectAll
, wxID_SELECTALL
} ,
392 { kHICommandClear
, wxID_CLEAR
} ,
393 { kHICommandUndo
, wxID_UNDO
} ,
394 { kHICommandRedo
, wxID_REDO
} ,
397 int wxMacCommandToId( UInt32 macCommandId
)
401 switch ( macCommandId
)
403 case kHICommandPreferences
:
404 wxid
= wxApp::s_macPreferencesMenuItemId
;
407 case kHICommandQuit
:
408 wxid
= wxApp::s_macExitMenuItemId
;
411 case kHICommandAbout
:
412 wxid
= wxApp::s_macAboutMenuItemId
;
417 for ( size_t i
= 0 ; i
< WXSIZEOF(gCommandIds
) ; ++i
)
419 if ( gCommandIds
[i
].macId
== macCommandId
)
421 wxid
= gCommandIds
[i
].wxId
;
430 wxid
= (int) macCommandId
;
435 UInt32
wxIdToMacCommand( int wxId
)
439 if ( wxId
== wxApp::s_macPreferencesMenuItemId
)
440 macId
= kHICommandPreferences
;
441 else if (wxId
== wxApp::s_macExitMenuItemId
)
442 macId
= kHICommandQuit
;
443 else if (wxId
== wxApp::s_macAboutMenuItemId
)
444 macId
= kHICommandAbout
;
447 for ( size_t i
= 0 ; i
< WXSIZEOF(gCommandIds
) ; ++i
)
449 if ( gCommandIds
[i
].wxId
== wxId
)
451 macId
= gCommandIds
[i
].macId
;
463 wxMenu
* wxFindMenuFromMacCommand( const HICommand
&command
, wxMenuItem
* &item
)
465 wxMenu
* itemMenu
= NULL
;
466 #ifndef __WXUNIVERSAL__
469 // for 'standard' commands which don't have a wx-menu
470 if ( command
.commandID
== kHICommandPreferences
|| command
.commandID
== kHICommandQuit
|| command
.commandID
== kHICommandAbout
)
472 id
= wxMacCommandToId( command
.commandID
) ;
474 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
476 item
= mbar
->FindItem( id
, &itemMenu
) ;
478 else if ( command
.commandID
!= 0 && command
.menu
.menuRef
!= 0 && command
.menu
.menuItemIndex
!= 0 )
480 id
= wxMacCommandToId( command
.commandID
) ;
481 // make sure it is one of our own menus, or of the 'synthetic' apple and help menus , otherwise don't touch
482 MenuItemIndex firstUserHelpMenuItem
;
483 static MenuHandle helpMenuHandle
= NULL
;
484 if ( helpMenuHandle
== NULL
)
486 if ( UMAGetHelpMenuDontCreate( &helpMenuHandle
, &firstUserHelpMenuItem
) != noErr
)
487 helpMenuHandle
= NULL
;
490 // is it part of the application or the Help menu, then look for the id directly
491 if ( ( GetMenuHandle( kwxMacAppleMenuId
) != NULL
&& command
.menu
.menuRef
== GetMenuHandle( kwxMacAppleMenuId
) ) ||
492 ( helpMenuHandle
!= NULL
&& command
.menu
.menuRef
== helpMenuHandle
) ||
493 wxMenuBar::MacGetWindowMenuHMenu() != NULL
&& command
.menu
.menuRef
== wxMenuBar::MacGetWindowMenuHMenu() )
495 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
497 item
= mbar
->FindItem( id
, &itemMenu
) ;
503 GetMenuItemRefCon( command
.menu
.menuRef
, command
.menu
.menuItemIndex
, &refCon
) ;
504 itemMenu
= wxFindMenuFromMacMenu( command
.menu
.menuRef
) ;
505 if ( itemMenu
!= NULL
)
506 item
= (wxMenuItem
*) refCon
;
513 //----------------------------------------------------------------------
514 // Carbon Event Handler
515 //----------------------------------------------------------------------
517 static const EventTypeSpec eventList
[] =
519 { kEventClassCommand
, kEventProcessCommand
} ,
520 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
522 { kEventClassMenu
, kEventMenuOpening
},
523 { kEventClassMenu
, kEventMenuClosed
},
524 { kEventClassMenu
, kEventMenuTargetItem
},
526 { kEventClassApplication
, kEventAppActivated
} ,
527 { kEventClassApplication
, kEventAppDeactivated
} ,
528 // handling the quit event is not recommended by apple
529 // rather using the quit apple event - which we do
531 { kEventClassAppleEvent
, kEventAppleEvent
} ,
533 { kEventClassMouse
, kEventMouseDown
} ,
534 { kEventClassMouse
, kEventMouseMoved
} ,
535 { kEventClassMouse
, kEventMouseUp
} ,
536 { kEventClassMouse
, kEventMouseDragged
} ,
540 static pascal OSStatus
541 wxMacAppMenuEventHandler( EventHandlerCallRef
WXUNUSED(handler
),
543 void *WXUNUSED(data
) )
545 wxMacCarbonEvent
cEvent( event
) ;
546 MenuRef menuRef
= cEvent
.GetParameter
<MenuRef
>(kEventParamDirectObject
) ;
547 #ifndef __WXUNIVERSAL__
548 wxMenu
* menu
= wxFindMenuFromMacMenu( menuRef
) ;
554 switch (GetEventKind(event
))
556 case kEventMenuOpening
:
557 type
= wxEVT_MENU_OPEN
;
560 case kEventMenuClosed
:
561 type
= wxEVT_MENU_CLOSE
;
564 case kEventMenuTargetItem
:
565 cmd
= cEvent
.GetParameter
<MenuCommand
>(kEventParamMenuCommand
,typeMenuCommand
) ;
567 type
= wxEVT_MENU_HIGHLIGHT
;
571 wxFAIL_MSG(wxT("Unexpected menu event kind"));
577 wxMenuEvent
wxevent(type
, cmd
, menu
);
578 wxevent
.SetEventObject(menu
);
580 wxEvtHandler
* handler
= menu
->GetEventHandler();
581 if (handler
&& handler
->ProcessEvent(wxevent
))
587 wxWindow
*win
= menu
->GetInvokingWindow();
589 win
->GetEventHandler()->ProcessEvent(wxevent
);
594 return eventNotHandledErr
;
598 static pascal OSStatus
599 wxMacAppCommandEventHandler( EventHandlerCallRef
WXUNUSED(handler
) ,
601 void *WXUNUSED(data
) )
603 OSStatus result
= eventNotHandledErr
;
607 wxMacCarbonEvent
cEvent( event
) ;
608 cEvent
.GetParameter
<HICommand
>(kEventParamDirectObject
,typeHICommand
,&command
) ;
610 wxMenuItem
* item
= NULL
;
611 wxMenu
* itemMenu
= wxFindMenuFromMacCommand( command
, item
) ;
612 int id
= wxMacCommandToId( command
.commandID
) ;
616 wxASSERT( itemMenu
!= NULL
) ;
618 switch ( cEvent
.GetKind() )
620 case kEventProcessCommand
:
621 result
= itemMenu
->MacHandleCommandProcess( item
, id
);
624 case kEventCommandUpdateStatus
:
625 result
= itemMenu
->MacHandleCommandUpdateStatus( item
, id
);
636 static pascal OSStatus
637 wxMacAppApplicationEventHandler( EventHandlerCallRef
WXUNUSED(handler
) ,
639 void *WXUNUSED(data
) )
641 OSStatus result
= eventNotHandledErr
;
642 switch ( GetEventKind( event
) )
644 case kEventAppActivated
:
646 wxTheApp
->SetActive( true , NULL
) ;
650 case kEventAppDeactivated
:
652 wxTheApp
->SetActive( false , NULL
) ;
663 pascal OSStatus
wxMacAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
665 EventRef formerEvent
= (EventRef
) wxTheApp
->MacGetCurrentEvent() ;
666 EventHandlerCallRef formerEventHandlerCallRef
= (EventHandlerCallRef
) wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
667 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
669 OSStatus result
= eventNotHandledErr
;
670 switch ( GetEventClass( event
) )
673 case kEventClassCommand
:
674 result
= wxMacAppCommandEventHandler( handler
, event
, data
) ;
677 case kEventClassApplication
:
678 result
= wxMacAppApplicationEventHandler( handler
, event
, data
) ;
681 case kEventClassMenu
:
682 result
= wxMacAppMenuEventHandler( handler
, event
, data
) ;
685 case kEventClassMouse
:
687 wxMacCarbonEvent
cEvent( event
) ;
690 Point screenMouseLocation
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ;
691 ::FindWindow(screenMouseLocation
, &window
);
692 // only send this event in case it had not already been sent to a tlw, as we get
693 // double events otherwise (in case event.skip) was called
694 if ( window
== NULL
)
695 result
= wxMacTopLevelMouseEventHandler( handler
, event
, NULL
) ;
699 case kEventClassAppleEvent
:
701 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
702 if ( AEProcessEvent
!= NULL
)
704 result
= AEProcessEvent(event
);
707 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
711 wxMacConvertEventToRecord( event
, &rec
) ;
712 result
= AEProcessAppleEvent( &rec
) ;
722 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerEventHandlerCallRef
) ;
727 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler
)
732 wxMacAssertOutputHandler(OSType
WXUNUSED(componentSignature
),
733 UInt32
WXUNUSED(options
),
734 const char *assertionString
,
735 const char *exceptionLabelString
,
736 const char *errorString
,
737 const char *fileName
,
740 ConstStr255Param
WXUNUSED(outputMsg
))
742 // flow into assert handling
743 wxString fileNameStr
;
744 wxString assertionStr
;
745 wxString exceptionStr
;
749 fileNameStr
= wxString(fileName
, wxConvLocal
);
750 assertionStr
= wxString(assertionString
, wxConvLocal
);
751 exceptionStr
= wxString((exceptionLabelString
!=0) ? exceptionLabelString
: "", wxConvLocal
) ;
752 errorStr
= wxString((errorString
!=0) ? errorString
: "", wxConvLocal
) ;
754 fileNameStr
= fileName
;
755 assertionStr
= assertionString
;
756 exceptionStr
= (exceptionLabelString
!=0) ? exceptionLabelString
: "" ;
757 errorStr
= (errorString
!=0) ? errorString
: "" ;
762 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
763 assertionStr
.c_str() ,
764 exceptionStr
.c_str() ,
766 fileNameStr
.c_str(), lineNumber
,
770 wxOnAssert(fileNameStr
, lineNumber
, assertionStr
,
771 wxString::Format( wxT("%s %s value (%p)") , exceptionStr
, errorStr
, value
) ) ;
777 extern "C" void macPostedEventCallback(void *WXUNUSED(unused
))
779 wxTheApp
->ProcessPendingEvents();
782 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
787 InstallDebugAssertOutputHandler( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler
) );
790 UMAInitToolbox( 4, sm_isEmbedded
) ;
791 // TODO CHECK Can Be Removed SetEventMask( everyEvent ) ;
793 // Mac OS X passes a process serial number command line argument when
794 // the application is launched from the Finder. This argument must be
795 // removed from the command line arguments before being handled by the
796 // application (otherwise applications would need to handle it)
799 static const wxChar
*ARG_PSN
= _T("-psn_");
800 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
802 // remove this argument
804 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
808 if ( !wxAppBase::Initialize(argc
, argv
) )
812 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
815 // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
816 wxString startupCwd
= wxGetCwd() ;
817 if ( startupCwd
== wxT("/") || startupCwd
.Right(15) == wxT("/Contents/MacOS") )
819 CFURLRef url
= CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
820 CFURLRef urlParent
= CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault
, url
) ;
822 CFStringRef path
= CFURLCopyFileSystemPath ( urlParent
, kCFURLPOSIXPathStyle
) ;
823 CFRelease( urlParent
) ;
824 wxString cwd
= wxMacCFStringHolder(path
).AsString(wxLocale::GetSystemEncoding());
825 wxSetWorkingDirectory( cwd
) ;
828 /* connect posted events to common-mode run loop so that wxPostEvent events
829 are handled even while we're in the menu or on a scrollbar */
830 CFRunLoopSourceContext event_posted_context
= {0};
831 event_posted_context
.perform
= macPostedEventCallback
;
832 m_macEventPosted
= CFRunLoopSourceCreate(NULL
,0,&event_posted_context
);
833 CFRunLoopAddSource(CFRunLoopGetCurrent(), m_macEventPosted
, kCFRunLoopCommonModes
);
834 // run loop takes ownership
835 CFRelease(m_macEventPosted
);
840 AEEventHandlerUPP sODocHandler
= NULL
;
841 AEEventHandlerUPP sGURLHandler
= NULL
;
842 AEEventHandlerUPP sOAppHandler
= NULL
;
843 AEEventHandlerUPP sPDocHandler
= NULL
;
844 AEEventHandlerUPP sRAppHandler
= NULL
;
845 AEEventHandlerUPP sQuitHandler
= NULL
;
847 bool wxApp::OnInitGui()
849 if ( !wxAppBase::OnInitGui() )
852 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
855 InstallApplicationEventHandler(
856 GetwxMacAppEventHandlerUPP(),
857 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
863 sODocHandler
= NewAEEventHandlerUPP(AEHandleODoc
) ;
864 sGURLHandler
= NewAEEventHandlerUPP(AEHandleGURL
) ;
865 sOAppHandler
= NewAEEventHandlerUPP(AEHandleOApp
) ;
866 sPDocHandler
= NewAEEventHandlerUPP(AEHandlePDoc
) ;
867 sRAppHandler
= NewAEEventHandlerUPP(AEHandleRApp
) ;
868 sQuitHandler
= NewAEEventHandlerUPP(AEHandleQuit
) ;
870 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
871 sODocHandler
, 0 , FALSE
) ;
872 AEInstallEventHandler( kInternetEventClass
, kAEGetURL
,
873 sGURLHandler
, 0 , FALSE
) ;
874 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
875 sOAppHandler
, 0 , FALSE
) ;
876 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
877 sPDocHandler
, 0 , FALSE
) ;
878 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
879 sRAppHandler
, 0 , FALSE
) ;
880 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
881 sQuitHandler
, 0 , FALSE
) ;
884 if ( !wxMacInitCocoa() )
890 void wxApp::CleanUp()
893 wxToolTip::RemoveToolTips() ;
896 if (m_macEventPosted
)
898 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), m_macEventPosted
, kCFRunLoopCommonModes
);
899 m_macEventPosted
= NULL
;
902 // One last chance for pending objects to be cleaned up
903 wxTheApp
->DeletePendingObjects();
906 RemoveEventHandler( (EventHandlerRef
)(wxTheApp
->m_macEventHandler
) );
910 AERemoveEventHandler( kCoreEventClass
, kAEOpenDocuments
,
911 sODocHandler
, FALSE
) ;
912 AERemoveEventHandler( kInternetEventClass
, kAEGetURL
,
913 sGURLHandler
, FALSE
) ;
914 AERemoveEventHandler( kCoreEventClass
, kAEOpenApplication
,
915 sOAppHandler
, FALSE
) ;
916 AERemoveEventHandler( kCoreEventClass
, kAEPrintDocuments
,
917 sPDocHandler
, FALSE
) ;
918 AERemoveEventHandler( kCoreEventClass
, kAEReopenApplication
,
919 sRAppHandler
, FALSE
) ;
920 AERemoveEventHandler( kCoreEventClass
, kAEQuitApplication
,
921 sQuitHandler
, FALSE
) ;
923 DisposeAEEventHandlerUPP( sODocHandler
) ;
924 DisposeAEEventHandlerUPP( sGURLHandler
) ;
925 DisposeAEEventHandlerUPP( sOAppHandler
) ;
926 DisposeAEEventHandlerUPP( sPDocHandler
) ;
927 DisposeAEEventHandlerUPP( sRAppHandler
) ;
928 DisposeAEEventHandlerUPP( sQuitHandler
) ;
931 wxAppBase::CleanUp();
934 //----------------------------------------------------------------------
935 // misc initialization stuff
936 //----------------------------------------------------------------------
938 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
939 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
941 OSStatus err
= noErr
;
942 bool converted
= ConvertEventRefToEventRecord( event
, rec
) ;
946 switch ( GetEventClass( event
) )
948 case kEventClassKeyboard
:
951 switch ( GetEventKind(event
) )
953 case kEventRawKeyDown
:
954 rec
->what
= keyDown
;
957 case kEventRawKeyRepeat
:
958 rec
->what
= autoKey
;
961 case kEventRawKeyUp
:
965 case kEventRawKeyModifiersChanged
:
966 rec
->what
= nullEvent
;
977 unsigned char charCode
;
979 GetMouse( &rec
->where
) ;
980 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
981 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
982 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
983 rec
->modifiers
= modifiers
;
984 rec
->message
= (keyCode
<< 8 ) + charCode
;
989 case kEventClassTextInput
:
991 switch ( GetEventKind( event
) )
993 case kEventTextInputUnicodeForKeyEvent
:
996 err
= GetEventParameter(
997 event
, kEventParamTextInputSendKeyboardEvent
, typeEventRef
, NULL
,
998 sizeof(rawEvent
), NULL
, &rawEvent
) ;
1002 UInt32 keyCode
, modifiers
;
1003 unsigned char charCode
;
1004 GetMouse( &rec
->where
) ;
1005 rec
->what
= keyDown
;
1006 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1007 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1008 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1009 rec
->modifiers
= modifiers
;
1010 rec
->message
= (keyCode
<< 8 ) + charCode
;
1032 m_printMode
= wxPRINT_WINDOWS
;
1034 m_macCurrentEvent
= NULL
;
1035 m_macCurrentEventHandlerCallRef
= NULL
;
1036 m_macEventPosted
= NULL
;
1039 void wxApp::OnIdle(wxIdleEvent
& WXUNUSED(event
))
1041 // If they are pending events, we must process them: pending events are
1042 // either events to the threads other than main or events posted with
1043 // wxPostEvent() functions
1044 #ifndef __WXUNIVERSAL__
1045 if (!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1046 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1050 void wxApp::WakeUpIdle()
1052 if (m_macEventPosted
)
1054 CFRunLoopSourceSignal(m_macEventPosted
);
1060 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1063 GetTopWindow()->Close(true);
1066 // Default behaviour: close the application with prompts. The
1067 // user can veto the close, and therefore the end session.
1068 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1072 if (!GetTopWindow()->Close(!event
.CanVeto()))
1077 extern "C" void wxCYield() ;
1083 // Yield to other processes
1085 bool wxApp::Yield(bool onlyIfNeeded
)
1089 if ( !onlyIfNeeded
)
1091 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1098 // Yielding from a non-gui thread needs to bail out, otherwise we end up
1099 // possibly sending events in the thread too.
1100 if ( !wxThread::IsMain() )
1104 #endif // wxUSE_THREADS
1108 // by definition yield should handle all non-processed events
1112 OSStatus status
= noErr
;
1114 while ( status
== noErr
)
1116 s_inReceiveEvent
= true ;
1117 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1118 s_inReceiveEvent
= false ;
1120 if ( status
== eventLoopTimedOutErr
)
1122 // make sure next time the event loop will trigger idle events
1123 sleepTime
= kEventDurationNoWait
;
1125 else if ( status
== eventLoopQuitErr
)
1127 // according to QA1061 this may also occur when a WakeUp Process
1132 MacHandleOneEvent( theEvent
) ;
1133 ReleaseEvent(theEvent
);
1142 void wxApp::MacDoOneEvent()
1144 wxMacAutoreleasePool autoreleasepool
;
1147 s_inReceiveEvent
= true ;
1148 OSStatus status
= ReceiveNextEvent(0, NULL
, sleepTime
, true, &theEvent
) ;
1149 s_inReceiveEvent
= false ;
1153 case eventLoopTimedOutErr
:
1154 if ( wxTheApp
->ProcessIdle() )
1155 sleepTime
= kEventDurationNoWait
;
1157 sleepTime
= kEventDurationSecond
;
1160 case eventLoopQuitErr
:
1161 // according to QA1061 this may also occur
1162 // when a WakeUp Process is executed
1166 MacHandleOneEvent( theEvent
) ;
1167 ReleaseEvent( theEvent
);
1168 sleepTime
= kEventDurationNoWait
;
1173 DeletePendingObjects() ;
1177 void wxApp::MacHandleUnhandledEvent( WXEVENTREF
WXUNUSED(evr
) )
1179 // Override to process unhandled events as you please
1182 CFMutableArrayRef
GetAutoReleaseArray()
1184 static CFMutableArrayRef array
= 0;
1186 array
= CFArrayCreateMutable(kCFAllocatorDefault
,0,&kCFTypeArrayCallBacks
);
1190 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1192 EventTargetRef theTarget
;
1193 theTarget
= GetEventDispatcherTarget();
1194 m_macCurrentEvent
= evr
;
1196 OSStatus status
= SendEventToEventTarget((EventRef
) evr
, theTarget
);
1197 if (status
== eventNotHandledErr
)
1198 MacHandleUnhandledEvent(evr
);
1201 wxMutexGuiLeaveOrEnter();
1202 #endif // wxUSE_THREADS
1204 CFArrayRemoveAllValues( GetAutoReleaseArray() );
1207 void wxApp::MacAddToAutorelease( void* cfrefobj
)
1209 CFArrayAppendValue( GetAutoReleaseArray(), cfrefobj
);
1212 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1217 case kHomeCharCode
:
1221 case kEnterCharCode
:
1222 retval
= WXK_RETURN
;
1228 case kHelpCharCode
:
1232 case kBackspaceCharCode
:
1240 case kPageUpCharCode
:
1241 retval
= WXK_PAGEUP
;
1244 case kPageDownCharCode
:
1245 retval
= WXK_PAGEDOWN
;
1248 case kReturnCharCode
:
1249 retval
= WXK_RETURN
;
1252 case kFunctionKeyCharCode
:
1322 case kEscapeCharCode
:
1323 retval
= WXK_ESCAPE
;
1326 case kLeftArrowCharCode
:
1330 case kRightArrowCharCode
:
1331 retval
= WXK_RIGHT
;
1334 case kUpArrowCharCode
:
1338 case kDownArrowCharCode
:
1342 case kDeleteCharCode
:
1343 retval
= WXK_DELETE
;
1353 int wxMacKeyCodeToModifier(wxKeyCode key
)
1378 wxMouseState
wxGetMouseState()
1382 wxPoint pt
= wxGetMousePosition();
1386 UInt32 buttons
= GetCurrentButtonState();
1387 ms
.SetLeftDown( (buttons
& 0x01) != 0 );
1388 ms
.SetMiddleDown( (buttons
& 0x04) != 0 );
1389 ms
.SetRightDown( (buttons
& 0x02) != 0 );
1391 UInt32 modifiers
= GetCurrentKeyModifiers();
1392 ms
.SetControlDown(modifiers
& controlKey
);
1393 ms
.SetShiftDown(modifiers
& shiftKey
);
1394 ms
.SetAltDown(modifiers
& optionKey
);
1395 ms
.SetMetaDown(modifiers
& cmdKey
);
1400 // TODO : once the new key/char handling is tested, move all the code to wxWindow
1402 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1408 wxKeyEvent
event(wxEVT_KEY_DOWN
) ;
1409 MacCreateKeyEvent( event
, focus
, keymessage
, modifiers
, when
, wherex
, wherey
, uniChar
) ;
1411 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1412 if ( handled
&& event
.GetSkipped() )
1418 wxWindow
*ancestor
= focus
;
1421 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1424 wxEvtHandler
* const handler
= ancestor
->GetEventHandler();
1426 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1427 handled
= handler
->ProcessEvent( command_event
);
1431 // accelerators can also be used with buttons, try them too
1432 command_event
.SetEventType(wxEVT_COMMAND_BUTTON_CLICKED
);
1433 handled
= handler
->ProcessEvent( command_event
);
1439 if (ancestor
->IsTopLevel())
1442 ancestor
= ancestor
->GetParent();
1445 #endif // wxUSE_ACCEL
1450 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1456 wxKeyEvent
event( wxEVT_KEY_UP
) ;
1457 MacCreateKeyEvent( event
, focus
, keymessage
, modifiers
, when
, wherex
, wherey
, uniChar
) ;
1458 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1463 bool wxApp::MacSendCharEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1468 wxKeyEvent
event(wxEVT_CHAR
) ;
1469 MacCreateKeyEvent( event
, focus
, keymessage
, modifiers
, when
, wherex
, wherey
, uniChar
) ;
1470 long keyval
= event
.m_keyCode
;
1472 bool handled
= false ;
1474 wxTopLevelWindowMac
*tlw
= focus
->MacGetTopLevelWindow() ;
1478 event
.SetEventType( wxEVT_CHAR_HOOK
);
1479 handled
= tlw
->GetEventHandler()->ProcessEvent( event
);
1480 if ( handled
&& event
.GetSkipped() )
1486 event
.SetEventType( wxEVT_CHAR
);
1487 event
.Skip( false ) ;
1488 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1491 if ( !handled
&& (keyval
== WXK_TAB
) )
1493 wxWindow
* iter
= focus
->GetParent() ;
1494 while ( iter
&& !handled
)
1496 if ( iter
->HasFlag( wxTAB_TRAVERSAL
) )
1498 wxNavigationKeyEvent new_event
;
1499 new_event
.SetEventObject( focus
);
1500 new_event
.SetDirection( !event
.ShiftDown() );
1501 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1502 new_event
.SetWindowChange( event
.ControlDown() );
1503 new_event
.SetCurrentFocus( focus
);
1504 handled
= focus
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
1505 if ( handled
&& new_event
.GetSkipped() )
1509 iter
= iter
->GetParent() ;
1513 // backdoor handler for default return and command escape
1514 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1516 // if window is not having a focus still testing for default enter or cancel
1517 // TODO: add the UMA version for ActiveNonFloatingWindow
1519 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1522 if ( keyval
== WXK_RETURN
|| keyval
== WXK_NUMPAD_ENTER
)
1524 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(focus
), wxTopLevelWindow
);
1525 if ( tlw
&& tlw
->GetDefaultItem() )
1527 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
1528 if ( def
&& def
->IsEnabled() )
1530 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1531 event
.SetEventObject(def
);
1532 def
->Command(event
);
1538 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1540 // generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs)
1541 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1542 new_event
.SetEventObject( focus
);
1543 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1551 // This method handles common code for SendKeyDown, SendKeyUp, and SendChar events.
1552 void wxApp::MacCreateKeyEvent( wxKeyEvent
& event
, wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1554 short keycode
, keychar
;
1556 keychar
= short(keymessage
& charCodeMask
);
1557 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1558 if ( !(event
.GetEventType() == wxEVT_CHAR
) && (modifiers
& (controlKey
| shiftKey
| optionKey
) ) )
1560 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1561 // and look at the character after
1563 // TODO new implementation using TextInputSources
1566 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
| shiftKey
| optionKey
))) | keycode
, &state
);
1567 keychar
= short(keyInfo
& charCodeMask
);
1571 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1572 if ( keyval
== keychar
&& ( event
.GetEventType() == wxEVT_KEY_UP
|| event
.GetEventType() == wxEVT_KEY_DOWN
) )
1573 keyval
= wxToupper( keyval
) ;
1575 // Check for NUMPAD keys. For KEY_UP/DOWN events we need to use the
1576 // WXK_NUMPAD constants, but for the CHAR event we want to use the
1577 // standard ascii values
1578 if ( event
.GetEventType() != wxEVT_CHAR
)
1580 if (keyval
>= '0' && keyval
<= '9' && keycode
>= 82 && keycode
<= 92)
1582 keyval
= (keyval
- '0') + WXK_NUMPAD0
;
1584 else if (keycode
>= 65 && keycode
<= 81)
1589 keyval
= WXK_NUMPAD_ENTER
;
1593 keyval
= WXK_NUMPAD_EQUAL
;
1597 keyval
= WXK_NUMPAD_MULTIPLY
;
1601 keyval
= WXK_NUMPAD_DIVIDE
;
1605 keyval
= WXK_NUMPAD_SUBTRACT
;
1609 keyval
= WXK_NUMPAD_ADD
;
1613 keyval
= WXK_NUMPAD_DECIMAL
;
1621 event
.m_shiftDown
= modifiers
& shiftKey
;
1622 event
.m_controlDown
= modifiers
& controlKey
;
1623 event
.m_altDown
= modifiers
& optionKey
;
1624 event
.m_metaDown
= modifiers
& cmdKey
;
1625 event
.m_keyCode
= keyval
;
1627 event
.m_uniChar
= uniChar
;
1630 event
.m_rawCode
= keymessage
;
1631 event
.m_rawFlags
= modifiers
;
1634 event
.SetTimestamp(when
);
1635 event
.SetEventObject(focus
);