1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/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"
42 #include "wx/thread.h"
43 #include "wx/evtloop.h"
49 #include "wx/osx/uma.h"
51 #include "wx/osx/private.h"
54 #if defined(WXMAKINGDLL_CORE)
55 # include <mach-o/dyld.h>
58 // Keep linker from discarding wxStockGDIMac
59 wxFORCE_LINK_MODULE(gdiobj
)
61 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
62 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
63 EVT_IDLE(wxApp::OnIdle
)
64 EVT_END_SESSION(wxApp::OnEndSession
)
65 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
69 // platform specific static variables
70 static const short kwxMacAppleMenuId
= 1 ;
72 wxWindow
* wxApp::s_captureWindow
= NULL
;
73 long wxApp::s_lastModifiers
= 0 ;
75 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
76 long wxApp::s_macPreferencesMenuItemId
= wxID_PREFERENCES
;
77 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
78 wxString
wxApp::s_macHelpMenuTitleName
= wxT("&Help") ;
80 bool wxApp::sm_isEmbedded
= false; // Normally we're not a plugin
84 //----------------------------------------------------------------------
85 // Core Apple Event Support
86 //----------------------------------------------------------------------
88 AEEventHandlerUPP sODocHandler
= NULL
;
89 AEEventHandlerUPP sGURLHandler
= NULL
;
90 AEEventHandlerUPP sOAppHandler
= NULL
;
91 AEEventHandlerUPP sPDocHandler
= NULL
;
92 AEEventHandlerUPP sRAppHandler
= NULL
;
93 AEEventHandlerUPP sQuitHandler
= NULL
;
95 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon refcon
) ;
96 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon refcon
) ;
97 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon refcon
) ;
98 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon refcon
) ;
99 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon refcon
) ;
100 pascal OSErr
AEHandleGURL( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon refcon
) ;
102 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon
WXUNUSED(refcon
) )
104 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
107 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon
WXUNUSED(refcon
) )
109 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
112 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon
WXUNUSED(refcon
) )
114 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
117 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon
WXUNUSED(refcon
) )
119 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
122 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon
WXUNUSED(refcon
) )
124 return wxTheApp
->MacHandleAERApp( (AppleEvent
*) event
, reply
) ;
127 pascal OSErr
AEHandleGURL( const AppleEvent
*event
, AppleEvent
*reply
, SRefCon
WXUNUSED(refcon
) )
129 return wxTheApp
->MacHandleAEGURL((WXEVENTREF
*)event
, reply
) ;
133 // AEODoc Calls MacOpenFile on each of the files passed
135 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
139 DescType returnedType
;
145 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
149 err
= AECountItems(&docList
, &itemsInList
);
153 ProcessSerialNumber PSN
;
154 PSN
.highLongOfPSN
= 0 ;
155 PSN
.lowLongOfPSN
= kCurrentProcess
;
156 SetFrontProcess( &PSN
) ;
161 for (i
= 1; i
<= itemsInList
; i
++)
164 &docList
, i
, typeFSRef
, &keywd
, &returnedType
,
165 (Ptr
)&theRef
, sizeof(theRef
), &actualSize
);
166 fName
= wxMacFSRefToPath( &theRef
) ;
174 // AEODoc Calls MacOpenURL on the url passed
176 short wxApp::MacHandleAEGURL(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
178 DescType returnedType
;
181 OSErr err
= AEGetParamPtr((AppleEvent
*)event
, keyDirectObject
, typeChar
,
182 &returnedType
, url
, sizeof(url
)-1,
187 url
[actualSize
] = '\0'; // Terminate the C string
189 ProcessSerialNumber PSN
;
190 PSN
.highLongOfPSN
= 0 ;
191 PSN
.lowLongOfPSN
= kCurrentProcess
;
192 SetFrontProcess( &PSN
) ;
194 MacOpenURL(wxString(url
, wxConvUTF8
));
199 // AEPDoc Calls MacPrintFile on each of the files passed
201 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
205 DescType returnedType
;
211 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
215 err
= AECountItems(&docList
, &itemsInList
);
219 ProcessSerialNumber PSN
;
220 PSN
.highLongOfPSN
= 0 ;
221 PSN
.lowLongOfPSN
= kCurrentProcess
;
222 SetFrontProcess( &PSN
) ;
227 for (i
= 1; i
<= itemsInList
; i
++)
230 &docList
, i
, typeFSRef
, &keywd
, &returnedType
,
231 (Ptr
)&theRef
, sizeof(theRef
), &actualSize
);
232 fName
= wxMacFSRefToPath( &theRef
) ;
240 // AEOApp calls MacNewFile
242 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
248 // AEQuit attempts to quit the application
250 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
253 wxTheApp
->OnQueryEndSession(event
);
254 if ( !event
.GetVeto() )
257 wxTheApp
->OnEndSession(event
);
262 // AEROApp calls MacReopenApp
264 short wxApp::MacHandleAERApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
273 //----------------------------------------------------------------------
274 // Support Routines linking the Mac...File Calls to the Document Manager
275 //----------------------------------------------------------------------
277 void wxApp::MacOpenFile(const wxString
& fileName
)
279 #if wxUSE_DOC_VIEW_ARCHITECTURE
280 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
282 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
286 void wxApp::MacOpenURL(const wxString
& WXUNUSED(url
) )
290 void wxApp::MacPrintFile(const wxString
& fileName
)
292 #if wxUSE_DOC_VIEW_ARCHITECTURE
294 #if wxUSE_PRINTING_ARCHITECTURE
295 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
298 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
301 wxView
* view
= doc
->GetFirstView() ;
304 wxPrintout
*printout
= view
->OnCreatePrintout();
308 printer
.Print(view
->GetFrame(), printout
, true);
315 doc
->DeleteAllViews();
316 dm
->RemoveDocument(doc
) ;
327 void wxApp::MacNewFile()
331 void wxApp::MacReopenApp()
334 // if there is no open window -> create a new one
335 // if all windows are hidden -> show the first
336 // if some windows are not hidden -> do nothing
338 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
345 wxTopLevelWindow
* firstIconized
= NULL
;
346 wxTopLevelWindow
* firstHidden
= NULL
;
349 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->GetData();
350 if ( !win
->IsShown() )
352 // make sure we don't show 'virtual toplevel windows' like wxTaskBarIconWindow
353 if ( firstHidden
== NULL
&& ( wxDynamicCast( win
, wxFrame
) || wxDynamicCast( win
, wxDialog
) ) )
356 else if ( win
->IsIconized() )
358 if ( firstIconized
== NULL
)
359 firstIconized
= win
;
363 // we do have a visible, non-iconized toplevelwindow -> do nothing
367 node
= node
->GetNext();
371 firstIconized
->Iconize( false ) ;
372 else if ( firstHidden
)
373 firstHidden
->Show( true );
377 //----------------------------------------------------------------------
378 // Macintosh CommandID support - converting between native and wx IDs
379 //----------------------------------------------------------------------
381 // if no native match they just return the passed-in id
391 IdPair gCommandIds
[] =
393 { kHICommandCut
, wxID_CUT
} ,
394 { kHICommandCopy
, wxID_COPY
} ,
395 { kHICommandPaste
, wxID_PASTE
} ,
396 { kHICommandSelectAll
, wxID_SELECTALL
} ,
397 { kHICommandClear
, wxID_CLEAR
} ,
398 { kHICommandUndo
, wxID_UNDO
} ,
399 { kHICommandRedo
, wxID_REDO
} ,
402 int wxMacCommandToId( UInt32 macCommandId
)
406 switch ( macCommandId
)
408 case kHICommandPreferences
:
409 wxid
= wxApp::s_macPreferencesMenuItemId
;
412 case kHICommandQuit
:
413 wxid
= wxApp::s_macExitMenuItemId
;
416 case kHICommandAbout
:
417 wxid
= wxApp::s_macAboutMenuItemId
;
422 for ( size_t i
= 0 ; i
< WXSIZEOF(gCommandIds
) ; ++i
)
424 if ( gCommandIds
[i
].macId
== macCommandId
)
426 wxid
= gCommandIds
[i
].wxId
;
435 wxid
= (int) macCommandId
;
440 UInt32
wxIdToMacCommand( int wxId
)
444 if ( wxId
== wxApp::s_macPreferencesMenuItemId
)
445 macId
= kHICommandPreferences
;
446 else if (wxId
== wxApp::s_macExitMenuItemId
)
447 macId
= kHICommandQuit
;
448 else if (wxId
== wxApp::s_macAboutMenuItemId
)
449 macId
= kHICommandAbout
;
452 for ( size_t i
= 0 ; i
< WXSIZEOF(gCommandIds
) ; ++i
)
454 if ( gCommandIds
[i
].wxId
== wxId
)
456 macId
= gCommandIds
[i
].macId
;
468 wxMenu
* wxFindMenuFromMacCommand( const HICommand
&command
, wxMenuItem
* &item
)
470 wxMenu
* itemMenu
= NULL
;
471 #ifndef __WXUNIVERSAL__
474 // for 'standard' commands which don't have a wx-menu
475 if ( command
.commandID
== kHICommandPreferences
|| command
.commandID
== kHICommandQuit
|| command
.commandID
== kHICommandAbout
)
477 id
= wxMacCommandToId( command
.commandID
) ;
479 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
481 item
= mbar
->FindItem( id
, &itemMenu
) ;
483 else if ( command
.commandID
!= 0 && command
.menu
.menuRef
!= 0 && command
.menu
.menuItemIndex
!= 0 )
485 id
= wxMacCommandToId( command
.commandID
) ;
486 // make sure it is one of our own menus, or of the 'synthetic' apple and help menus , otherwise don't touch
487 MenuItemIndex firstUserHelpMenuItem
;
488 static MenuHandle helpMenuHandle
= NULL
;
489 if ( helpMenuHandle
== NULL
)
491 if ( UMAGetHelpMenuDontCreate( &helpMenuHandle
, &firstUserHelpMenuItem
) != noErr
)
492 helpMenuHandle
= NULL
;
495 // is it part of the application or the Help menu, then look for the id directly
496 if ( ( GetMenuHandle( kwxMacAppleMenuId
) != NULL
&& command
.menu
.menuRef
== GetMenuHandle( kwxMacAppleMenuId
) ) ||
497 ( helpMenuHandle
!= NULL
&& command
.menu
.menuRef
== helpMenuHandle
) ||
498 wxMenuBar::MacGetWindowMenuHMenu() != NULL
&& command
.menu
.menuRef
== wxMenuBar::MacGetWindowMenuHMenu() )
500 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
502 item
= mbar
->FindItem( id
, &itemMenu
) ;
508 GetMenuItemRefCon( command
.menu
.menuRef
, command
.menu
.menuItemIndex
, &refCon
) ;
509 itemMenu
= wxFindMenuFromMacMenu( command
.menu
.menuRef
) ;
510 if ( itemMenu
!= NULL
&& refCon
!= 0)
511 item
= (wxMenuItem
*) refCon
;
520 //----------------------------------------------------------------------
521 // Carbon Event Handler
522 //----------------------------------------------------------------------
526 static const EventTypeSpec eventList
[] =
528 { kEventClassCommand
, kEventProcessCommand
} ,
529 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
531 { kEventClassMenu
, kEventMenuOpening
},
532 { kEventClassMenu
, kEventMenuClosed
},
533 { kEventClassMenu
, kEventMenuTargetItem
},
535 { kEventClassApplication
, kEventAppActivated
} ,
536 { kEventClassApplication
, kEventAppDeactivated
} ,
537 // handling the quit event is not recommended by apple
538 // rather using the quit apple event - which we do
540 { kEventClassAppleEvent
, kEventAppleEvent
} ,
542 { kEventClassMouse
, kEventMouseDown
} ,
543 { kEventClassMouse
, kEventMouseMoved
} ,
544 { kEventClassMouse
, kEventMouseUp
} ,
545 { kEventClassMouse
, kEventMouseDragged
} ,
549 static pascal OSStatus
550 wxMacAppMenuEventHandler( EventHandlerCallRef
WXUNUSED(handler
),
552 void *WXUNUSED(data
) )
554 wxMacCarbonEvent
cEvent( event
) ;
555 MenuRef menuRef
= cEvent
.GetParameter
<MenuRef
>(kEventParamDirectObject
) ;
556 #ifndef __WXUNIVERSAL__
557 wxMenu
* menu
= wxFindMenuFromMacMenu( menuRef
) ;
561 switch (GetEventKind(event
))
563 case kEventMenuOpening
:
564 menu
->HandleMenuOpened();
567 case kEventMenuClosed
:
568 menu
->HandleMenuClosed();
571 case kEventMenuTargetItem
:
575 command
.menu
.menuRef
= menuRef
;
576 command
.menu
.menuItemIndex
= cEvent
.GetParameter
<MenuItemIndex
>(kEventParamMenuItemIndex
,typeMenuItemIndex
) ;
577 command
.commandID
= cEvent
.GetParameter
<MenuCommand
>(kEventParamMenuCommand
,typeMenuCommand
) ;
578 if (command
.commandID
!= 0)
580 wxMenuItem
* item
= NULL
;
581 wxMenu
* itemMenu
= wxFindMenuFromMacCommand( command
, item
) ;
582 if ( itemMenu
&& item
)
583 itemMenu
->HandleMenuItemHighlighted( item
);
589 wxFAIL_MSG(wxT("Unexpected menu event kind"));
595 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
) ;
615 wxASSERT( itemMenu
!= NULL
) ;
617 switch ( cEvent
.GetKind() )
619 case kEventProcessCommand
:
620 if ( itemMenu
->HandleCommandProcess( item
) )
624 case kEventCommandUpdateStatus
:
625 if ( itemMenu
->HandleCommandUpdateStatus( item
) )
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
)
730 #if wxDEBUG_LEVEL && wxOSX_USE_COCOA_OR_CARBON
733 wxMacAssertOutputHandler(OSType
WXUNUSED(componentSignature
),
734 UInt32
WXUNUSED(options
),
735 const char *assertionString
,
736 const char *exceptionLabelString
,
737 const char *errorString
,
738 const char *fileName
,
741 ConstStr255Param
WXUNUSED(outputMsg
))
743 // flow into assert handling
744 wxString fileNameStr
;
745 wxString assertionStr
;
746 wxString exceptionStr
;
750 fileNameStr
= wxString(fileName
, wxConvLocal
);
751 assertionStr
= wxString(assertionString
, wxConvLocal
);
752 exceptionStr
= wxString((exceptionLabelString
!=0) ? exceptionLabelString
: "", wxConvLocal
) ;
753 errorStr
= wxString((errorString
!=0) ? errorString
: "", wxConvLocal
) ;
755 fileNameStr
= fileName
;
756 assertionStr
= assertionString
;
757 exceptionStr
= (exceptionLabelString
!=0) ? exceptionLabelString
: "" ;
758 errorStr
= (errorString
!=0) ? errorString
: "" ;
763 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
764 assertionStr
.c_str() ,
765 exceptionStr
.c_str() ,
767 fileNameStr
.c_str(), lineNumber
,
771 wxOnAssert(fileNameStr
, lineNumber
, assertionStr
,
772 wxString::Format( wxT("%s %s value (%p)") , exceptionStr
, errorStr
, value
) ) ;
776 #endif // wxDEBUG_LEVEL
778 extern "C" void macPostedEventCallback(void *WXUNUSED(unused
))
780 wxTheApp
->ProcessPendingEvents();
783 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
787 #if wxDEBUG_LEVEL && wxOSX_USE_COCOA_OR_CARBON
788 InstallDebugAssertOutputHandler( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler
) );
791 // Mac OS X passes a process serial number command line argument when
792 // the application is launched from the Finder. This argument must be
793 // removed from the command line arguments before being handled by the
794 // application (otherwise applications would need to handle it)
797 static const wxChar
*ARG_PSN
= wxT("-psn_");
798 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
800 // remove this argument
802 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
806 if ( !wxAppBase::Initialize(argc
, argv
) )
810 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
813 // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
814 wxString startupCwd
= wxGetCwd() ;
815 if ( startupCwd
== wxT("/") || startupCwd
.Right(15) == wxT("/Contents/MacOS") )
817 CFURLRef url
= CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
818 CFURLRef urlParent
= CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault
, url
) ;
820 CFStringRef path
= CFURLCopyFileSystemPath ( urlParent
, kCFURLPOSIXPathStyle
) ;
821 CFRelease( urlParent
) ;
822 wxString cwd
= wxCFStringRef(path
).AsString(wxLocale::GetSystemEncoding());
823 wxSetWorkingDirectory( cwd
) ;
826 /* connect posted events to common-mode run loop so that wxPostEvent events
827 are handled even while we're in the menu or on a scrollbar */
829 CFRunLoopSourceContext event_posted_context = {0};
830 event_posted_context.perform = macPostedEventCallback;
831 m_macEventPosted = CFRunLoopSourceCreate(NULL,0,&event_posted_context);
832 CFRunLoopAddSource(CFRunLoopGetCurrent(), m_macEventPosted, kCFRunLoopCommonModes);
833 // run loop takes ownership
834 CFRelease(m_macEventPosted);
839 #if wxOSX_USE_COCOA_OR_CARBON
840 bool wxApp::CallOnInit()
842 wxMacAutoreleasePool autoreleasepool
;
847 bool wxApp::OnInitGui()
849 if ( !wxAppBase::OnInitGui() )
858 bool wxApp::ProcessIdle()
860 wxMacAutoreleasePool autoreleasepool
;
861 return wxAppBase::ProcessIdle();
866 wxMacAutoreleasePool pool
;
867 return wxAppBase::OnRun();
871 bool wxApp::DoInitGui()
873 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
876 InstallApplicationEventHandler(
877 GetwxMacAppEventHandlerUPP(),
878 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
883 sODocHandler
= NewAEEventHandlerUPP(AEHandleODoc
) ;
884 sGURLHandler
= NewAEEventHandlerUPP(AEHandleGURL
) ;
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( kInternetEventClass
, kAEGetURL
,
893 sGURLHandler
, 0 , FALSE
) ;
894 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
895 sOAppHandler
, 0 , FALSE
) ;
896 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
897 sPDocHandler
, 0 , FALSE
) ;
898 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
899 sRAppHandler
, 0 , FALSE
) ;
900 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
901 sQuitHandler
, 0 , FALSE
) ;
904 if ( !wxMacInitCocoa() )
910 void wxApp::DoCleanUp()
913 RemoveEventHandler( (EventHandlerRef
)(wxTheApp
->m_macEventHandler
) );
917 AERemoveEventHandler( kCoreEventClass
, kAEOpenDocuments
,
918 sODocHandler
, FALSE
) ;
919 AERemoveEventHandler( kInternetEventClass
, kAEGetURL
,
920 sGURLHandler
, FALSE
) ;
921 AERemoveEventHandler( kCoreEventClass
, kAEOpenApplication
,
922 sOAppHandler
, FALSE
) ;
923 AERemoveEventHandler( kCoreEventClass
, kAEPrintDocuments
,
924 sPDocHandler
, FALSE
) ;
925 AERemoveEventHandler( kCoreEventClass
, kAEReopenApplication
,
926 sRAppHandler
, FALSE
) ;
927 AERemoveEventHandler( kCoreEventClass
, kAEQuitApplication
,
928 sQuitHandler
, FALSE
) ;
930 DisposeAEEventHandlerUPP( sODocHandler
) ;
931 DisposeAEEventHandlerUPP( sGURLHandler
) ;
932 DisposeAEEventHandlerUPP( sOAppHandler
) ;
933 DisposeAEEventHandlerUPP( sPDocHandler
) ;
934 DisposeAEEventHandlerUPP( sRAppHandler
) ;
935 DisposeAEEventHandlerUPP( sQuitHandler
) ;
941 void wxApp::CleanUp()
943 wxMacAutoreleasePool autoreleasepool
;
945 wxToolTip::RemoveToolTips() ;
948 if (m_macEventPosted
)
950 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), m_macEventPosted
, kCFRunLoopCommonModes
);
951 m_macEventPosted
= NULL
;
956 wxAppBase::CleanUp();
959 //----------------------------------------------------------------------
960 // misc initialization stuff
961 //----------------------------------------------------------------------
963 #if wxOSX_USE_CARBON && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
964 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
966 OSStatus err
= noErr
;
967 bool converted
= ConvertEventRefToEventRecord( event
, rec
) ;
971 switch ( GetEventClass( event
) )
973 case kEventClassKeyboard
:
976 switch ( GetEventKind(event
) )
978 case kEventRawKeyDown
:
979 rec
->what
= keyDown
;
982 case kEventRawKeyRepeat
:
983 rec
->what
= autoKey
;
986 case kEventRawKeyUp
:
990 case kEventRawKeyModifiersChanged
:
991 rec
->what
= nullEvent
;
1002 unsigned char charCode
;
1004 GetMouse( &rec
->where
) ;
1005 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1006 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1007 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1008 rec
->modifiers
= modifiers
;
1009 rec
->message
= (keyCode
<< 8 ) + charCode
;
1014 case kEventClassTextInput
:
1016 switch ( GetEventKind( event
) )
1018 case kEventTextInputUnicodeForKeyEvent
:
1021 err
= GetEventParameter(
1022 event
, kEventParamTextInputSendKeyboardEvent
, typeEventRef
, NULL
,
1023 sizeof(rawEvent
), NULL
, &rawEvent
) ;
1027 UInt32 keyCode
, modifiers
;
1028 unsigned char charCode
;
1029 GetMouse( &rec
->where
) ;
1030 rec
->what
= keyDown
;
1031 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1032 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1033 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1034 rec
->modifiers
= modifiers
;
1035 rec
->message
= (keyCode
<< 8 ) + charCode
;
1057 m_printMode
= wxPRINT_WINDOWS
;
1059 m_macCurrentEvent
= NULL
;
1060 m_macCurrentEventHandlerCallRef
= NULL
;
1061 m_macEventPosted
= NULL
;
1062 m_macPool
= new wxMacAutoreleasePool();
1071 CFMutableArrayRef
GetAutoReleaseArray()
1073 static CFMutableArrayRef array
= 0;
1075 array
= CFArrayCreateMutable(kCFAllocatorDefault
,0,&kCFTypeArrayCallBacks
);
1079 void wxApp::MacAddToAutorelease( void* cfrefobj
)
1081 CFArrayAppendValue( GetAutoReleaseArray(), cfrefobj
);
1084 void wxApp::MacReleaseAutoreleasePool()
1088 m_macPool
= new wxMacAutoreleasePool();
1091 void wxApp::OnIdle(wxIdleEvent
& WXUNUSED(event
))
1093 // If they are pending events, we must process them: pending events are
1094 // either events to the threads other than main or events posted with
1095 // wxPostEvent() functions
1096 #ifndef __WXUNIVERSAL__
1098 if (!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1099 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1102 CFArrayRemoveAllValues( GetAutoReleaseArray() );
1105 void wxApp::WakeUpIdle()
1107 if (m_macEventPosted
)
1109 CFRunLoopSourceSignal(m_macEventPosted
);
1115 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1118 GetTopWindow()->Close(true);
1121 // Default behaviour: close the application with prompts. The
1122 // user can veto the close, and therefore the end session.
1123 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1125 if ( !wxDialog::OSXHasModalDialogsOpen() )
1129 if (!GetTopWindow()->Close(!event
.CanVeto()))
1139 extern "C" void wxCYield() ;
1146 void wxApp::MacHandleUnhandledEvent( WXEVENTREF
WXUNUSED(evr
) )
1148 // Override to process unhandled events as you please
1151 #if wxOSX_USE_CARBON
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
:
1263 case kEscapeCharCode
:
1264 retval
= WXK_ESCAPE
;
1267 case kLeftArrowCharCode
:
1271 case kRightArrowCharCode
:
1272 retval
= WXK_RIGHT
;
1275 case kUpArrowCharCode
:
1279 case kDownArrowCharCode
:
1283 case kDeleteCharCode
:
1284 retval
= WXK_DELETE
;
1294 int wxMacKeyCodeToModifier(wxKeyCode key
)
1320 #if wxOSX_USE_COCOA && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6
1322 // defined in utils.mm
1324 #elif wxOSX_USE_COCOA_OR_CARBON
1326 wxMouseState
wxGetMouseState()
1330 wxPoint pt
= wxGetMousePosition();
1334 UInt32 buttons
= GetCurrentButtonState();
1335 ms
.SetLeftDown( (buttons
& 0x01) != 0 );
1336 ms
.SetMiddleDown( (buttons
& 0x04) != 0 );
1337 ms
.SetRightDown( (buttons
& 0x02) != 0 );
1339 UInt32 modifiers
= GetCurrentKeyModifiers();
1340 ms
.SetControlDown(modifiers
& controlKey
);
1341 ms
.SetShiftDown(modifiers
& shiftKey
);
1342 ms
.SetAltDown(modifiers
& optionKey
);
1343 ms
.SetMetaDown(modifiers
& cmdKey
);
1350 // TODO : once the new key/char handling is tested, move all the code to wxWindow
1352 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1357 wxKeyEvent
event(wxEVT_KEY_DOWN
) ;
1358 MacCreateKeyEvent( event
, focus
, keymessage
, modifiers
, when
, wherex
, wherey
, uniChar
) ;
1360 return focus
->OSXHandleKeyEvent(event
);
1363 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1368 wxKeyEvent
event( wxEVT_KEY_UP
) ;
1369 MacCreateKeyEvent( event
, focus
, keymessage
, modifiers
, when
, wherex
, wherey
, uniChar
) ;
1371 return focus
->OSXHandleKeyEvent(event
) ;
1374 bool wxApp::MacSendCharEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1378 wxKeyEvent
event(wxEVT_CHAR
) ;
1379 MacCreateKeyEvent( event
, focus
, keymessage
, modifiers
, when
, wherex
, wherey
, uniChar
) ;
1381 bool handled
= false ;
1383 #if wxOSX_USE_CARBON
1384 long keyval
= event
.m_keyCode
;
1385 wxNonOwnedWindow
*tlw
= focus
->MacGetTopLevelWindow() ;
1389 event
.SetEventType( wxEVT_CHAR_HOOK
);
1390 handled
= tlw
->HandleWindowEvent( event
);
1391 if ( handled
&& event
.GetSkipped() )
1397 event
.SetEventType( wxEVT_CHAR
);
1398 event
.Skip( false ) ;
1399 handled
= focus
->HandleWindowEvent( event
) ;
1402 if ( !handled
&& (keyval
== WXK_TAB
) )
1404 wxWindow
* iter
= focus
->GetParent() ;
1405 while ( iter
&& !handled
)
1407 if ( iter
->HasFlag( wxTAB_TRAVERSAL
) )
1409 wxNavigationKeyEvent new_event
;
1410 new_event
.SetEventObject( focus
);
1411 new_event
.SetDirection( !event
.ShiftDown() );
1412 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1413 new_event
.SetWindowChange( event
.ControlDown() );
1414 new_event
.SetCurrentFocus( focus
);
1415 handled
= focus
->GetParent()->HandleWindowEvent( new_event
);
1416 if ( handled
&& new_event
.GetSkipped() )
1420 iter
= iter
->GetParent() ;
1424 // backdoor handler for default return and command escape
1425 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->AcceptsFocus() ) )
1427 // if window is not having a focus still testing for default enter or cancel
1428 // TODO: add the UMA version for ActiveNonFloatingWindow
1430 wxWindow
* focus
= wxNonOwnedWindow::GetFromWXWindow( (WXWindow
) FrontWindow() ) ;
1433 if ( keyval
== WXK_RETURN
|| keyval
== WXK_NUMPAD_ENTER
)
1435 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(focus
), wxTopLevelWindow
);
1436 if ( tlw
&& tlw
->GetDefaultItem() )
1438 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
1439 if ( def
&& def
->IsEnabled() )
1441 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1442 event
.SetEventObject(def
);
1443 def
->Command(event
);
1449 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1451 // generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs)
1452 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1453 new_event
.SetEventObject( focus
);
1454 handled
= focus
->HandleWindowEvent( new_event
);
1463 // This method handles common code for SendKeyDown, SendKeyUp, and SendChar events.
1464 void wxApp::MacCreateKeyEvent( wxKeyEvent
& event
, wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1466 #if wxOSX_USE_CARBON
1467 short keycode
, keychar
;
1469 keychar
= short(keymessage
& charCodeMask
);
1470 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1471 if ( !(event
.GetEventType() == wxEVT_CHAR
) && (modifiers
& (controlKey
| shiftKey
| optionKey
) ) )
1473 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1474 // and look at the character after
1476 // TODO new implementation using TextInputSources
1479 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
| shiftKey
| optionKey
))) | keycode
, &state
);
1480 keychar
= short(keyInfo
& charCodeMask
);
1484 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1485 if ( keyval
== keychar
&& ( event
.GetEventType() == wxEVT_KEY_UP
|| event
.GetEventType() == wxEVT_KEY_DOWN
) )
1486 keyval
= wxToupper( keyval
) ;
1488 // Check for NUMPAD keys. For KEY_UP/DOWN events we need to use the
1489 // WXK_NUMPAD constants, but for the CHAR event we want to use the
1490 // standard ascii values
1491 if ( event
.GetEventType() != wxEVT_CHAR
)
1493 if (keyval
>= '0' && keyval
<= '9' && keycode
>= 82 && keycode
<= 92)
1495 keyval
= (keyval
- '0') + WXK_NUMPAD0
;
1497 else if (keycode
>= 65 && keycode
<= 81)
1502 keyval
= WXK_NUMPAD_ENTER
;
1506 keyval
= WXK_NUMPAD_EQUAL
;
1510 keyval
= WXK_NUMPAD_MULTIPLY
;
1514 keyval
= WXK_NUMPAD_DIVIDE
;
1518 keyval
= WXK_NUMPAD_SUBTRACT
;
1522 keyval
= WXK_NUMPAD_ADD
;
1526 keyval
= WXK_NUMPAD_DECIMAL
;
1534 event
.m_shiftDown
= modifiers
& shiftKey
;
1535 event
.m_controlDown
= modifiers
& controlKey
;
1536 event
.m_altDown
= modifiers
& optionKey
;
1537 event
.m_metaDown
= modifiers
& cmdKey
;
1538 event
.m_keyCode
= keyval
;
1540 event
.m_uniChar
= uniChar
;
1543 event
.m_rawCode
= keymessage
;
1544 event
.m_rawFlags
= modifiers
;
1547 event
.SetTimestamp(when
);
1548 event
.SetEventObject(focus
);
1552 wxUnusedVar(keymessage
);
1553 wxUnusedVar(modifiers
);
1555 wxUnusedVar(wherex
);
1556 wxUnusedVar(wherey
);
1557 wxUnusedVar(uniChar
);
1562 void wxApp::MacHideApp()
1564 #if wxOSX_USE_CARBON
1565 wxMacCarbonEvent
event( kEventClassCommand
, kEventCommandProcess
);
1567 memset( &command
, 0 , sizeof(command
) );
1568 command
.commandID
= kHICommandHide
;
1569 event
.SetParameter
<HICommand
>(kEventParamDirectObject
, command
);
1570 SendEventToApplication( event
);