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
;
69 extern size_t g_numberOfThreads
;
72 // statics for implementation
73 static bool s_inYield
= false;
74 static bool s_inReceiveEvent
= false ;
75 static EventTime sleepTime
= kEventDurationNoWait
;
78 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
79 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
80 EVT_IDLE(wxApp::OnIdle
)
81 EVT_END_SESSION(wxApp::OnEndSession
)
82 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
86 // platform specific static variables
87 const short kMacMinHeap
= (29 * 1024) ;
88 const short kwxMacMenuBarResource
= 1 ;
89 const short kwxMacAppleMenuId
= 1 ;
91 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
92 wxWindow
* wxApp::s_captureWindow
= NULL
;
93 int wxApp::s_lastMouseDown
= 0 ;
94 long wxApp::sm_lastMessageTime
= 0;
95 long wxApp::s_lastModifiers
= 0 ;
97 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
98 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
99 long wxApp::s_macPreferencesMenuItemId
= wxID_PREFERENCES
;
100 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
101 wxString
wxApp::s_macHelpMenuTitleName
= wxT("&Help") ;
103 bool wxApp::sm_isEmbedded
= false; // Normally we're not a plugin
105 //----------------------------------------------------------------------
106 // Core Apple Event Support
107 //----------------------------------------------------------------------
109 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
110 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
111 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
112 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
113 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
115 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
117 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
120 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
122 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
125 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
127 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
130 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
132 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
135 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
137 return wxTheApp
->MacHandleAERApp( (AppleEvent
*) event
, reply
) ;
140 // AEODoc Calls MacOpenFile on each of the files passed
142 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
146 DescType returnedType
;
152 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
156 err
= AECountItems(&docList
, &itemsInList
);
160 ProcessSerialNumber PSN
;
161 PSN
.highLongOfPSN
= 0 ;
162 PSN
.lowLongOfPSN
= kCurrentProcess
;
163 SetFrontProcess( &PSN
) ;
168 for (i
= 1; i
<= itemsInList
; i
++)
171 &docList
, i
, typeFSRef
, &keywd
, &returnedType
,
172 (Ptr
)&theRef
, sizeof(theRef
), &actualSize
);
173 fName
= wxMacFSRefToPath( &theRef
) ;
181 // AEPDoc Calls MacPrintFile on each of the files passed
183 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
187 DescType returnedType
;
193 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
197 err
= AECountItems(&docList
, &itemsInList
);
201 ProcessSerialNumber PSN
;
202 PSN
.highLongOfPSN
= 0 ;
203 PSN
.lowLongOfPSN
= kCurrentProcess
;
204 SetFrontProcess( &PSN
) ;
209 for (i
= 1; i
<= itemsInList
; i
++)
212 &docList
, i
, typeFSRef
, &keywd
, &returnedType
,
213 (Ptr
)&theRef
, sizeof(theRef
), &actualSize
);
214 fName
= wxMacFSRefToPath( &theRef
) ;
222 // AEOApp calls MacNewFile
224 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
230 // AEQuit attempts to quit the application
232 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
234 wxWindow
* win
= GetTopWindow() ;
237 wxCommandEvent
exitEvent(wxEVT_COMMAND_MENU_SELECTED
, s_macExitMenuItemId
);
238 if (!win
->ProcessEvent(exitEvent
))
249 // AEROApp calls MacReopenApp
251 short wxApp::MacHandleAERApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
258 //----------------------------------------------------------------------
259 // Support Routines linking the Mac...File Calls to the Document Manager
260 //----------------------------------------------------------------------
262 void wxApp::MacOpenFile(const wxString
& fileName
)
264 #if wxUSE_DOC_VIEW_ARCHITECTURE
265 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
267 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
272 void wxApp::MacPrintFile(const wxString
& fileName
)
274 #if wxUSE_DOC_VIEW_ARCHITECTURE
276 #if wxUSE_PRINTING_ARCHITECTURE
277 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
280 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
283 wxView
* view
= doc
->GetFirstView() ;
286 wxPrintout
*printout
= view
->OnCreatePrintout();
290 printer
.Print(view
->GetFrame(), printout
, true);
297 doc
->DeleteAllViews();
298 dm
->RemoveDocument(doc
) ;
309 void wxApp::MacNewFile()
313 void wxApp::MacReopenApp()
316 // if there is no open window -> create a new one
317 // if all windows are hidden -> show the first
318 // if some windows are not hidden -> do nothing
320 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
327 wxTopLevelWindow
* firstIconized
= NULL
;
330 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->GetData();
331 if ( !win
->IsIconized() )
333 firstIconized
= NULL
;
338 if ( firstIconized
== NULL
)
339 firstIconized
= win
;
342 node
= node
->GetNext();
346 firstIconized
->Iconize( false ) ;
350 //----------------------------------------------------------------------
351 // Macintosh CommandID support - converting between native and wx IDs
352 //----------------------------------------------------------------------
354 // if no native match they just return the passed-in id
362 IdPair gCommandIds
[] =
364 { kHICommandCut
, wxID_CUT
} ,
365 { kHICommandCopy
, wxID_COPY
} ,
366 { kHICommandPaste
, wxID_PASTE
} ,
367 { kHICommandSelectAll
, wxID_SELECTALL
} ,
368 { kHICommandClear
, wxID_CLEAR
} ,
369 { kHICommandUndo
, wxID_UNDO
} ,
370 { kHICommandRedo
, wxID_REDO
} ,
373 int wxMacCommandToId( UInt32 macCommandId
)
377 switch ( macCommandId
)
379 case kHICommandPreferences
:
380 wxid
= wxApp::s_macPreferencesMenuItemId
;
383 case kHICommandQuit
:
384 wxid
= wxApp::s_macExitMenuItemId
;
387 case kHICommandAbout
:
388 wxid
= wxApp::s_macAboutMenuItemId
;
393 for ( size_t i
= 0 ; i
< WXSIZEOF(gCommandIds
) ; ++i
)
395 if ( gCommandIds
[i
].macId
== macCommandId
)
397 wxid
= gCommandIds
[i
].wxId
;
406 wxid
= (int) macCommandId
;
411 UInt32
wxIdToMacCommand( int wxId
)
415 if ( wxId
== wxApp::s_macPreferencesMenuItemId
)
416 macId
= kHICommandPreferences
;
417 else if (wxId
== wxApp::s_macExitMenuItemId
)
418 macId
= kHICommandQuit
;
419 else if (wxId
== wxApp::s_macAboutMenuItemId
)
420 macId
= kHICommandAbout
;
423 for ( size_t i
= 0 ; i
< WXSIZEOF(gCommandIds
) ; ++i
)
425 if ( gCommandIds
[i
].wxId
== wxId
)
427 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() ;
451 item
= mbar
->FindItem( id
, &itemMenu
) ;
453 else if ( command
.commandID
!= 0 && command
.menu
.menuRef
!= 0 && command
.menu
.menuItemIndex
!= 0 )
455 id
= wxMacCommandToId( command
.commandID
) ;
456 // make sure it is one of our own menus, or of the 'synthetic' apple and help menus , otherwise don't touch
457 MenuItemIndex firstUserHelpMenuItem
;
458 static MenuHandle mh
= NULL
;
461 if ( UMAGetHelpMenu( &mh
, &firstUserHelpMenuItem
) != noErr
)
465 // is it part of the application or the Help menu, then look for the id directly
466 if ( ( GetMenuHandle( kwxMacAppleMenuId
) != NULL
&& command
.menu
.menuRef
== GetMenuHandle( kwxMacAppleMenuId
) ) ||
467 ( mh
!= NULL
&& command
.menu
.menuRef
== mh
) )
469 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
471 item
= mbar
->FindItem( id
, &itemMenu
) ;
477 GetMenuItemRefCon( command
.menu
.menuRef
, command
.menu
.menuItemIndex
, &refCon
) ;
478 itemMenu
= wxFindMenuFromMacMenu( command
.menu
.menuRef
) ;
479 if ( itemMenu
!= NULL
)
480 item
= (wxMenuItem
*) refCon
;
487 //----------------------------------------------------------------------
488 // Carbon Event Handler
489 //----------------------------------------------------------------------
491 static const EventTypeSpec eventList
[] =
493 { kEventClassCommand
, kEventProcessCommand
} ,
494 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
496 { kEventClassMenu
, kEventMenuOpening
},
497 { kEventClassMenu
, kEventMenuClosed
},
498 { kEventClassMenu
, kEventMenuTargetItem
},
500 { kEventClassApplication
, kEventAppActivated
} ,
501 { kEventClassApplication
, kEventAppDeactivated
} ,
502 // handling the quit event is not recommended by apple
503 // rather using the quit apple event - which we do
505 { kEventClassAppleEvent
, kEventAppleEvent
} ,
507 { kEventClassMouse
, kEventMouseDown
} ,
508 { kEventClassMouse
, kEventMouseMoved
} ,
509 { kEventClassMouse
, kEventMouseUp
} ,
510 { kEventClassMouse
, kEventMouseDragged
} ,
514 static pascal OSStatus
515 wxMacAppMenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
517 wxMacCarbonEvent
cEvent( event
) ;
518 MenuRef menuRef
= cEvent
.GetParameter
<MenuRef
>(kEventParamDirectObject
) ;
519 wxMenu
* menu
= wxFindMenuFromMacMenu( menuRef
) ;
525 switch (GetEventKind(event
))
527 case kEventMenuOpening
:
528 type
= wxEVT_MENU_OPEN
;
531 case kEventMenuClosed
:
532 type
= wxEVT_MENU_CLOSE
;
535 case kEventMenuTargetItem
:
536 cmd
= cEvent
.GetParameter
<MenuCommand
>(kEventParamMenuCommand
,typeMenuCommand
) ;
538 type
= wxEVT_MENU_HIGHLIGHT
;
542 wxFAIL_MSG(wxT("Unexpected menu event kind"));
548 wxMenuEvent
wxevent(type
, cmd
, menu
);
549 wxevent
.SetEventObject(menu
);
551 wxEvtHandler
* handler
= menu
->GetEventHandler();
552 if (handler
&& handler
->ProcessEvent(wxevent
))
558 wxWindow
*win
= menu
->GetInvokingWindow();
560 win
->GetEventHandler()->ProcessEvent(wxevent
);
565 return eventNotHandledErr
;
568 static pascal OSStatus
wxMacAppCommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
570 OSStatus result
= eventNotHandledErr
;
574 wxMacCarbonEvent
cEvent( event
) ;
575 cEvent
.GetParameter
<HICommand
>(kEventParamDirectObject
,typeHICommand
,&command
) ;
577 wxMenuItem
* item
= NULL
;
578 wxMenu
* itemMenu
= wxFindMenuFromMacCommand( command
, item
) ;
579 int id
= wxMacCommandToId( command
.commandID
) ;
583 wxASSERT( itemMenu
!= NULL
) ;
585 switch ( cEvent
.GetKind() )
587 case kEventProcessCommand
:
589 if (item
->IsCheckable())
590 item
->Check( !item
->IsChecked() ) ;
592 if ( itemMenu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) )
597 case kEventCommandUpdateStatus
:
599 wxUpdateUIEvent
event(id
);
600 event
.SetEventObject( itemMenu
);
602 bool processed
= false;
604 // Try the menu's event handler
606 wxEvtHandler
*handler
= itemMenu
->GetEventHandler();
608 processed
= handler
->ProcessEvent(event
);
611 // Try the window the menu was popped up from
612 // (and up through the hierarchy)
615 const wxMenuBase
*menu
= itemMenu
;
618 wxWindow
*win
= menu
->GetInvokingWindow();
621 processed
= win
->GetEventHandler()->ProcessEvent(event
);
625 menu
= menu
->GetParent();
631 // if anything changed, update the changed attribute
632 if (event
.GetSetText())
633 itemMenu
->SetLabel(id
, event
.GetText());
634 if (event
.GetSetChecked())
635 itemMenu
->Check(id
, event
.GetChecked());
636 if (event
.GetSetEnabled())
637 itemMenu
->Enable(id
, event
.GetEnabled());
651 static pascal OSStatus
wxMacAppApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
653 OSStatus result
= eventNotHandledErr
;
654 switch ( GetEventKind( event
) )
656 case kEventAppActivated
:
658 wxTheApp
->SetActive( true , NULL
) ;
662 case kEventAppDeactivated
:
664 wxTheApp
->SetActive( false , NULL
) ;
675 pascal OSStatus
wxMacAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
677 EventRef formerEvent
= (EventRef
) wxTheApp
->MacGetCurrentEvent() ;
678 EventHandlerCallRef formerEventHandlerCallRef
= (EventHandlerCallRef
) wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
679 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
681 OSStatus result
= eventNotHandledErr
;
682 switch ( GetEventClass( event
) )
684 case kEventClassCommand
:
685 result
= wxMacAppCommandEventHandler( handler
, event
, data
) ;
688 case kEventClassApplication
:
689 result
= wxMacAppApplicationEventHandler( handler
, event
, data
) ;
692 case kEventClassMenu
:
693 result
= wxMacAppMenuEventHandler( handler
, event
, data
) ;
696 case kEventClassMouse
:
698 wxMacCarbonEvent
cEvent( event
) ;
701 Point screenMouseLocation
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ;
702 ::FindWindow(screenMouseLocation
, &window
);
703 // only send this event in case it had not already been sent to a tlw, as we get
704 // double events otherwise (in case event.skip) was called
705 if ( window
== NULL
)
706 result
= wxMacTopLevelMouseEventHandler( handler
, event
, NULL
) ;
710 case kEventClassAppleEvent
:
714 wxMacConvertEventToRecord( event
, &rec
) ;
715 result
= AEProcessAppleEvent( &rec
) ;
723 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerEventHandlerCallRef
) ;
728 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler
)
730 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
731 // we know it's there ;-)
732 WXIMPORT
char std::__throws_bad_alloc
;
737 pascal static void wxMacAssertOutputHandler(OSType componentSignature
, UInt32 options
,
738 const char *assertionString
, const char *exceptionLabelString
,
739 const char *errorString
, const char *fileName
, long lineNumber
, void *value
, ConstStr255Param outputMsg
)
741 // flow into assert handling
742 wxString fileNameStr
;
743 wxString assertionStr
;
744 wxString exceptionStr
;
748 fileNameStr
= wxString(fileName
, wxConvLocal
);
749 assertionStr
= wxString(assertionString
, wxConvLocal
);
750 exceptionStr
= wxString((exceptionLabelString
!=0) ? exceptionLabelString
: "", wxConvLocal
) ;
751 errorStr
= wxString((errorString
!=0) ? errorString
: "", wxConvLocal
) ;
753 fileNameStr
= fileName
;
754 assertionStr
= assertionString
;
755 exceptionStr
= (exceptionLabelString
!=0) ? exceptionLabelString
: "" ;
756 errorStr
= (errorString
!=0) ? errorString
: "" ;
761 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
762 assertionStr
.c_str() ,
763 exceptionStr
.c_str() ,
765 fileNameStr
.c_str(), lineNumber
,
769 wxOnAssert(fileNameStr
, lineNumber
, assertionStr
,
770 wxString::Format( wxT("%s %s value (%p)") , exceptionStr
, errorStr
, value
) ) ;
779 // m_macEventPosted run loop source callback:
780 void macPostedEventCallback(void *unused
);
783 void macPostedEventCallback(void *unused
)
785 wxTheApp
->ProcessPendingEvents();
789 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
794 InstallDebugAssertOutputHandler( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler
) );
797 UMAInitToolbox( 4, sm_isEmbedded
) ;
798 SetEventMask( everyEvent
) ;
799 UMAShowWatchCursor() ;
802 # if __option(profile)
803 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
808 // now avoid exceptions thrown for new (bad_alloc)
809 // FIXME CS for some changes outside wxMac does not compile anymore
811 std::__throws_bad_alloc
= 0 ;
816 s_macCursorRgn
= ::NewRgn() ;
818 // Mac OS X passes a process serial number command line argument when
819 // the application is launched from the Finder. This argument must be
820 // removed from the command line arguments before being handled by the
821 // application (otherwise applications would need to handle it)
824 static const wxChar
*ARG_PSN
= _T("-psn_");
825 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
827 // remove this argument
829 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
833 if ( !wxAppBase::Initialize(argc
, argv
) )
837 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
840 #if TARGET_API_MAC_OSX
841 // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
842 wxString startupCwd
= wxGetCwd() ;
843 if ( startupCwd
== wxT("/") || startupCwd
.Right(15) == wxT("/Contents/MacOS") )
845 CFURLRef url
= CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
846 CFURLRef urlParent
= CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault
, url
) ;
848 CFStringRef path
= CFURLCopyFileSystemPath ( urlParent
, kCFURLPOSIXPathStyle
) ;
849 CFRelease( urlParent
) ;
850 wxString cwd
= wxMacCFStringHolder(path
).AsString(wxLocale::GetSystemEncoding());
851 wxSetWorkingDirectory( cwd
) ;
855 wxMacCreateNotifierTable() ;
858 /* connect posted events to common-mode run loop so that wxPostEvent events
859 are handled even while we're in the menu or on a scrollbar */
860 CFRunLoopSourceContext event_posted_context
= {0};
861 event_posted_context
.perform
= macPostedEventCallback
;
862 m_macEventPosted
= CFRunLoopSourceCreate(NULL
,0,&event_posted_context
);
863 CFRunLoopAddSource(CFRunLoopGetCurrent(), m_macEventPosted
, kCFRunLoopCommonModes
);
866 UMAShowArrowCursor() ;
871 AEEventHandlerUPP sODocHandler
= NULL
;
872 AEEventHandlerUPP sOAppHandler
= NULL
;
873 AEEventHandlerUPP sPDocHandler
= NULL
;
874 AEEventHandlerUPP sRAppHandler
= NULL
;
875 AEEventHandlerUPP sQuitHandler
= NULL
;
877 bool wxApp::OnInitGui()
879 if ( !wxAppBase::OnInitGui() )
882 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
886 InstallApplicationEventHandler(
887 GetwxMacAppEventHandlerUPP(),
888 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
893 sODocHandler
= NewAEEventHandlerUPP(AEHandleODoc
) ;
894 sOAppHandler
= NewAEEventHandlerUPP(AEHandleOApp
) ;
895 sPDocHandler
= NewAEEventHandlerUPP(AEHandlePDoc
) ;
896 sRAppHandler
= NewAEEventHandlerUPP(AEHandleRApp
) ;
897 sQuitHandler
= NewAEEventHandlerUPP(AEHandleQuit
) ;
899 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
900 sODocHandler
, 0 , FALSE
) ;
901 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
902 sOAppHandler
, 0 , FALSE
) ;
903 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
904 sPDocHandler
, 0 , FALSE
) ;
905 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
906 sRAppHandler
, 0 , FALSE
) ;
907 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
908 sQuitHandler
, 0 , FALSE
) ;
914 void wxApp::CleanUp()
917 wxToolTip::RemoveToolTips() ;
921 if (m_macEventPosted
)
922 CFRelease(m_macEventPosted
);
923 m_macEventPosted
= NULL
;
926 // One last chance for pending objects to be cleaned up
927 wxTheApp
->DeletePendingObjects();
929 wxMacDestroyNotifierTable() ;
932 # if __option(profile)
933 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
938 UMACleanupToolbox() ;
940 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
943 RemoveEventHandler( (EventHandlerRef
)(wxTheApp
->m_macEventHandler
) );
947 AERemoveEventHandler( kCoreEventClass
, kAEOpenDocuments
,
948 sODocHandler
, FALSE
) ;
949 AERemoveEventHandler( kCoreEventClass
, kAEOpenApplication
,
950 sOAppHandler
, FALSE
) ;
951 AERemoveEventHandler( kCoreEventClass
, kAEPrintDocuments
,
952 sPDocHandler
, FALSE
) ;
953 AERemoveEventHandler( kCoreEventClass
, kAEReopenApplication
,
954 sRAppHandler
, FALSE
) ;
955 AERemoveEventHandler( kCoreEventClass
, kAEQuitApplication
,
956 sQuitHandler
, FALSE
) ;
958 DisposeAEEventHandlerUPP( sODocHandler
) ;
959 DisposeAEEventHandlerUPP( sOAppHandler
) ;
960 DisposeAEEventHandlerUPP( sPDocHandler
) ;
961 DisposeAEEventHandlerUPP( sRAppHandler
) ;
962 DisposeAEEventHandlerUPP( sQuitHandler
) ;
965 wxAppBase::CleanUp();
968 //----------------------------------------------------------------------
969 // misc initialization stuff
970 //----------------------------------------------------------------------
972 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
974 // for shared libraries we have to manually get the correct resource
975 // ref num upon initializing and releasing when terminating, therefore
976 // the __wxinitialize and __wxterminate must be used
980 void __sinit(void); // (generated by linker)
981 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
982 pascal void __terminate(void);
985 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
987 return __initialize( theInitBlock
) ;
990 pascal void __wxterminate(void)
995 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
997 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
999 OSStatus err
= noErr
;
1000 bool converted
= ConvertEventRefToEventRecord( event
, rec
) ;
1004 switch ( GetEventClass( event
) )
1006 case kEventClassKeyboard
:
1009 switch ( GetEventKind(event
) )
1011 case kEventRawKeyDown
:
1012 rec
->what
= keyDown
;
1015 case kEventRawKeyRepeat
:
1016 rec
->what
= autoKey
;
1019 case kEventRawKeyUp
:
1023 case kEventRawKeyModifiersChanged
:
1024 rec
->what
= nullEvent
;
1035 unsigned char charCode
;
1037 GetMouse( &rec
->where
) ;
1039 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1040 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1041 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1042 rec
->modifiers
= modifiers
;
1043 rec
->message
= (keyCode
<< 8 ) + charCode
;
1048 case kEventClassTextInput
:
1050 switch ( GetEventKind( event
) )
1052 case kEventTextInputUnicodeForKeyEvent
:
1055 err
= GetEventParameter(
1056 event
, kEventParamTextInputSendKeyboardEvent
, typeEventRef
, NULL
,
1057 sizeof(rawEvent
), NULL
, &rawEvent
) ;
1061 UInt32 keyCode
, modifiers
;
1062 unsigned char charCode
;
1064 GetMouse( &rec
->where
) ;
1065 rec
->what
= keyDown
;
1066 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1067 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1068 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1069 rec
->modifiers
= modifiers
;
1070 rec
->message
= (keyCode
<< 8 ) + charCode
;
1091 m_printMode
= wxPRINT_WINDOWS
;
1093 m_macCurrentEvent
= NULL
;
1094 m_macCurrentEventHandlerCallRef
= NULL
;
1096 #ifdef __WXMAC_OSX__
1097 m_macEventPosted
= NULL
;
1101 void wxApp::OnIdle(wxIdleEvent
& event
)
1103 wxAppBase::OnIdle(event
);
1105 // If they are pending events, we must process them: pending events are
1106 // either events to the threads other than main or events posted with
1107 // wxPostEvent() functions
1108 wxMacProcessNotifierAndPendingEvents();
1110 if (!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1111 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1114 void wxApp::WakeUpIdle()
1116 #ifdef __WXMAC_OSX__
1117 if (m_macEventPosted
)
1119 CFRunLoopSourceSignal(m_macEventPosted
);
1126 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1129 GetTopWindow()->Close(true);
1132 // Default behaviour: close the application with prompts. The
1133 // user can veto the close, and therefore the end session.
1134 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1138 if (!GetTopWindow()->Close(!event
.CanVeto()))
1143 extern "C" void wxCYield() ;
1149 // Yield to other processes
1151 bool wxApp::Yield(bool onlyIfNeeded
)
1155 if ( !onlyIfNeeded
)
1157 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1165 // by definition yield should handle all non-processed events
1169 OSStatus status
= noErr
;
1171 while ( status
== noErr
)
1173 s_inReceiveEvent
= true ;
1174 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1175 s_inReceiveEvent
= false ;
1177 if ( status
== eventLoopTimedOutErr
)
1179 // make sure next time the event loop will trigger idle events
1180 sleepTime
= kEventDurationNoWait
;
1182 else if ( status
== eventLoopQuitErr
)
1184 // according to QA1061 this may also occur when a WakeUp Process
1189 MacHandleOneEvent( theEvent
) ;
1190 ReleaseEvent(theEvent
);
1194 wxMacProcessNotifierAndPendingEvents() ;
1200 void wxApp::MacDoOneEvent()
1204 s_inReceiveEvent
= true ;
1205 OSStatus status
= ReceiveNextEvent(0, NULL
, sleepTime
, true, &theEvent
) ;
1206 s_inReceiveEvent
= false ;
1210 case eventLoopTimedOutErr
:
1211 if ( wxTheApp
->ProcessIdle() )
1212 sleepTime
= kEventDurationNoWait
;
1214 sleepTime
= kEventDurationSecond
;
1217 case eventLoopQuitErr
:
1218 // according to QA1061 this may also occur
1219 // when a WakeUp Process is executed
1223 MacHandleOneEvent( theEvent
) ;
1224 ReleaseEvent( theEvent
);
1225 sleepTime
= kEventDurationNoWait
;
1230 DeletePendingObjects() ;
1231 wxMacProcessNotifierAndPendingEvents() ;
1235 void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1237 // Override to process unhandled events as you please
1240 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1242 EventTargetRef theTarget
;
1243 theTarget
= GetEventDispatcherTarget();
1244 m_macCurrentEvent
= evr
;
1246 OSStatus status
= SendEventToEventTarget((EventRef
) evr
, theTarget
);
1247 if (status
== eventNotHandledErr
)
1248 MacHandleUnhandledEvent(evr
);
1250 wxMacProcessNotifierAndPendingEvents() ;
1253 wxMutexGuiLeaveOrEnter();
1254 #endif // wxUSE_THREADS
1257 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1258 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1263 case kHomeCharCode
:
1267 case kEnterCharCode
:
1268 retval
= WXK_RETURN
;
1274 case kHelpCharCode
:
1278 case kBackspaceCharCode
:
1286 case kPageUpCharCode
:
1287 retval
= WXK_PAGEUP
;
1290 case kPageDownCharCode
:
1291 retval
= WXK_PAGEDOWN
;
1294 case kReturnCharCode
:
1295 retval
= WXK_RETURN
;
1298 case kFunctionKeyCharCode
:
1368 case kEscapeCharCode
:
1369 retval
= WXK_ESCAPE
;
1372 case kLeftArrowCharCode
:
1376 case kRightArrowCharCode
:
1377 retval
= WXK_RIGHT
;
1380 case kUpArrowCharCode
:
1384 case kDownArrowCharCode
:
1388 case kDeleteCharCode
:
1389 retval
= WXK_DELETE
;
1399 int wxMacKeyCodeToModifier(wxKeyCode key
)
1425 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1427 wxASSERT_MSG(key
!= WXK_LBUTTON
&& key
!= WXK_RBUTTON
&& key
!=
1428 WXK_MBUTTON
, wxT("can't use wxGetKeyState() for mouse buttons"));
1430 //if OS X > 10.2 (i.e. 10.2.x)
1431 //a known apple bug prevents the system from determining led
1432 //states with GetKeys... can only determine caps lock led
1433 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key
));
1435 // KeyMapByteArray keymap;
1436 // GetKeys((BigEndianLong*)keymap);
1437 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1442 wxMouseState
wxGetMouseState()
1446 wxPoint pt
= wxGetMousePosition();
1450 UInt32 buttons
= GetCurrentButtonState();
1451 ms
.SetLeftDown( (buttons
& 0x01) != 0 );
1452 ms
.SetMiddleDown( (buttons
& 0x04) != 0 );
1453 ms
.SetRightDown( (buttons
& 0x02) != 0 );
1455 UInt32 modifiers
= GetCurrentKeyModifiers();
1456 ms
.SetControlDown(modifiers
& controlKey
);
1457 ms
.SetShiftDown(modifiers
& shiftKey
);
1458 ms
.SetAltDown(modifiers
& optionKey
);
1459 ms
.SetMetaDown(modifiers
& cmdKey
);
1464 // TODO : once the new key/char handling is tested, move all the code to wxWindow
1466 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1472 wxKeyEvent
event(wxEVT_KEY_DOWN
) ;
1473 MacCreateKeyEvent( event
, focus
, keymessage
, modifiers
, when
, wherex
, wherey
, uniChar
) ;
1475 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1476 if ( handled
&& event
.GetSkipped() )
1482 wxWindow
*ancestor
= focus
;
1485 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1488 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1489 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1493 if (ancestor
->IsTopLevel())
1496 ancestor
= ancestor
->GetParent();
1499 #endif // wxUSE_ACCEL
1504 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1510 wxKeyEvent
event( wxEVT_KEY_UP
) ;
1511 MacCreateKeyEvent( event
, focus
, keymessage
, modifiers
, when
, wherex
, wherey
, uniChar
) ;
1512 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1517 bool wxApp::MacSendCharEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1522 wxKeyEvent
event(wxEVT_CHAR
) ;
1523 MacCreateKeyEvent( event
, focus
, keymessage
, modifiers
, when
, wherex
, wherey
, uniChar
) ;
1524 long keyval
= event
.m_keyCode
;
1526 bool handled
= false ;
1528 wxTopLevelWindowMac
*tlw
= focus
->MacGetTopLevelWindow() ;
1532 event
.SetEventType( wxEVT_CHAR_HOOK
);
1533 handled
= tlw
->GetEventHandler()->ProcessEvent( event
);
1534 if ( handled
&& event
.GetSkipped() )
1540 event
.SetEventType( wxEVT_CHAR
);
1541 event
.Skip( false ) ;
1542 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1545 if ( !handled
&& (keyval
== WXK_TAB
) )
1547 wxWindow
* iter
= focus
->GetParent() ;
1548 while ( iter
&& !handled
)
1550 if ( iter
->HasFlag( wxTAB_TRAVERSAL
) )
1552 wxNavigationKeyEvent new_event
;
1553 new_event
.SetEventObject( focus
);
1554 new_event
.SetDirection( !event
.ShiftDown() );
1555 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1556 new_event
.SetWindowChange( event
.ControlDown() );
1557 new_event
.SetCurrentFocus( focus
);
1558 handled
= focus
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
1559 if ( handled
&& new_event
.GetSkipped() )
1563 iter
= iter
->GetParent() ;
1567 // backdoor handler for default return and command escape
1568 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1570 // if window is not having a focus still testing for default enter or cancel
1571 // TODO: add the UMA version for ActiveNonFloatingWindow
1572 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1575 if ( keyval
== WXK_RETURN
)
1577 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(), wxButton
);
1578 if ( def
&& def
->IsEnabled() )
1580 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1581 event
.SetEventObject(def
);
1582 def
->Command(event
);
1587 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1589 // generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs)
1590 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1591 new_event
.SetEventObject( focus
);
1592 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1599 // This method handles common code for SendKeyDown, SendKeyUp, and SendChar events.
1600 void wxApp::MacCreateKeyEvent( wxKeyEvent
& event
, wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1602 short keycode
, keychar
;
1604 keychar
= short(keymessage
& charCodeMask
);
1605 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1606 if ( !(event
.GetEventType() == wxEVT_CHAR
) && (modifiers
& (controlKey
| shiftKey
| optionKey
) ) )
1608 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1609 // and look at the character after
1611 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
| shiftKey
| optionKey
))) | keycode
, &state
);
1612 keychar
= short(keyInfo
& charCodeMask
);
1615 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1616 if ( keyval
== keychar
&& ( event
.GetEventType() == wxEVT_KEY_UP
|| event
.GetEventType() == wxEVT_KEY_DOWN
) )
1617 keyval
= wxToupper( keyval
) ;
1619 // Check for NUMPAD keys
1620 if (keyval
>= '0' && keyval
<= '9' && keycode
>= 82 && keycode
<= 92)
1622 keyval
= (keyval
- '0') + WXK_NUMPAD0
;
1624 else if (keycode
>= 67 && keycode
<= 81)
1629 keyval
= WXK_NUMPAD_ENTER
;
1633 keyval
= WXK_NUMPAD_EQUAL
;
1637 keyval
= WXK_NUMPAD_MULTIPLY
;
1641 keyval
= WXK_NUMPAD_DIVIDE
;
1645 keyval
= WXK_NUMPAD_SUBTRACT
;
1649 keyval
= WXK_NUMPAD_ADD
;
1653 keyval
= WXK_NUMPAD_DECIMAL
;
1661 event
.m_shiftDown
= modifiers
& shiftKey
;
1662 event
.m_controlDown
= modifiers
& controlKey
;
1663 event
.m_altDown
= modifiers
& optionKey
;
1664 event
.m_metaDown
= modifiers
& cmdKey
;
1665 event
.m_keyCode
= keyval
;
1667 event
.m_uniChar
= uniChar
;
1668 if ( event
.GetEventType() == wxEVT_CHAR
)
1669 event
.m_keyCode
= uniChar
;
1672 event
.m_rawCode
= keymessage
;
1673 event
.m_rawFlags
= modifiers
;
1676 event
.SetTimestamp(when
);
1677 event
.SetEventObject(focus
);