1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/window.h"
16 #include "wx/button.h"
19 #include "wx/gdicmn.h"
22 #include "wx/cursor.h"
25 #include "wx/palette.h"
27 #include "wx/dialog.h"
28 #include "wx/msgdlg.h"
30 #include "wx/module.h"
31 #include "wx/memory.h"
32 #include "wx/tooltip.h"
33 #include "wx/textctrl.h"
35 #include "wx/docview.h"
36 #include "wx/filename.h"
48 // #include "apprsrc.h"
50 #include "wx/mac/uma.h"
51 #include "wx/mac/macnotfy.h"
54 # include <CoreServices/CoreServices.h>
55 # if defined(WXMAKINGDLL_CORE)
56 # include <mach-o/dyld.h>
61 # include <ToolUtils.h>
62 # include <DiskInit.h>
66 extern wxList wxPendingDelete
;
68 // set wxMAC_USE_RAEL to 1 if RunApplicationEventLoop should be used
69 // if 0 the lower level CarbonEventLoop will be used
70 // on the long run RAEL should replace the low level event loop
71 // we will have to clean up event handling to make sure we don't
72 // miss handling of things like pending events etc
73 // perhaps we will also have to pipe events through an ueber-event-handler
74 // to make sure we have one place to do all these house-keeping functions
76 #define wxMAC_USE_RAEL 0
79 extern size_t g_numberOfThreads
;
82 // statics for implementation
83 static bool s_inYield
= false;
84 static bool s_inReceiveEvent
= false ;
85 static EventTime sleepTime
= kEventDurationNoWait
;
88 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
89 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
90 EVT_IDLE(wxApp::OnIdle
)
91 EVT_END_SESSION(wxApp::OnEndSession
)
92 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
96 // platform specific static variables
97 const short kMacMinHeap
= (29 * 1024) ;
98 const short kwxMacMenuBarResource
= 1 ;
99 const short kwxMacAppleMenuId
= 1 ;
101 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
102 wxWindow
* wxApp::s_captureWindow
= NULL
;
103 int wxApp::s_lastMouseDown
= 0 ;
104 long wxApp::sm_lastMessageTime
= 0;
105 long wxApp::s_lastModifiers
= 0 ;
107 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
108 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
109 long wxApp::s_macPreferencesMenuItemId
= wxID_PREFERENCES
;
110 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
111 wxString
wxApp::s_macHelpMenuTitleName
= wxT("&Help") ;
113 bool wxApp::sm_isEmbedded
= false; // Normally we're not a plugin
115 //----------------------------------------------------------------------
116 // Core Apple Event Support
117 //----------------------------------------------------------------------
119 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
120 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
121 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
122 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
123 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
125 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
127 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
130 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
132 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
135 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
137 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
140 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
142 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
145 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
147 return wxTheApp
->MacHandleAERApp( (AppleEvent
*) event
, reply
) ;
150 // AEODoc Calls MacOpenFile on each of the files passed
152 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
156 DescType returnedType
;
162 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
166 err
= AECountItems(&docList
, &itemsInList
);
170 ProcessSerialNumber PSN
;
171 PSN
.highLongOfPSN
= 0 ;
172 PSN
.lowLongOfPSN
= kCurrentProcess
;
173 SetFrontProcess( &PSN
) ;
178 for (i
= 1; i
<= itemsInList
; i
++)
181 &docList
, i
, typeFSRef
, &keywd
, &returnedType
,
182 (Ptr
)&theRef
, sizeof(theRef
), &actualSize
);
183 fName
= wxMacFSRefToPath( &theRef
) ;
191 // AEPDoc Calls MacPrintFile on each of the files passed
193 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
197 DescType returnedType
;
203 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
207 err
= AECountItems(&docList
, &itemsInList
);
211 ProcessSerialNumber PSN
;
212 PSN
.highLongOfPSN
= 0 ;
213 PSN
.lowLongOfPSN
= kCurrentProcess
;
214 SetFrontProcess( &PSN
) ;
219 for (i
= 1; i
<= itemsInList
; i
++)
222 &docList
, i
, typeFSRef
, &keywd
, &returnedType
,
223 (Ptr
)&theRef
, sizeof(theRef
), &actualSize
);
224 fName
= wxMacFSRefToPath( &theRef
) ;
232 // AEOApp calls MacNewFile
234 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
240 // AEQuit attempts to quit the application
242 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
244 wxWindow
* win
= GetTopWindow() ;
247 wxCommandEvent
exitEvent(wxEVT_COMMAND_MENU_SELECTED
, s_macExitMenuItemId
);
248 if (!win
->ProcessEvent(exitEvent
))
259 // AEROApp calls MacReopenApp
261 short wxApp::MacHandleAERApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
268 //----------------------------------------------------------------------
269 // Support Routines linking the Mac...File Calls to the Document Manager
270 //----------------------------------------------------------------------
272 void wxApp::MacOpenFile(const wxString
& fileName
)
274 #if wxUSE_DOC_VIEW_ARCHITECTURE
275 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
277 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
282 void wxApp::MacPrintFile(const wxString
& fileName
)
284 #if wxUSE_DOC_VIEW_ARCHITECTURE
286 #if wxUSE_PRINTING_ARCHITECTURE
287 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
290 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
293 wxView
* view
= doc
->GetFirstView() ;
296 wxPrintout
*printout
= view
->OnCreatePrintout();
300 printer
.Print(view
->GetFrame(), printout
, true);
307 doc
->DeleteAllViews();
308 dm
->RemoveDocument(doc
) ;
319 void wxApp::MacNewFile()
323 void wxApp::MacReopenApp()
326 // if there is no open window -> create a new one
327 // if all windows are hidden -> show the first
328 // if some windows are not hidden -> do nothing
330 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
337 wxTopLevelWindow
* firstIconized
= NULL
;
340 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->GetData();
341 if ( !win
->IsIconized() )
343 firstIconized
= NULL
;
348 if ( firstIconized
== NULL
)
349 firstIconized
= win
;
352 node
= node
->GetNext();
356 firstIconized
->Iconize( false ) ;
360 //----------------------------------------------------------------------
361 // Macintosh CommandID support - converting between native and wx IDs
362 //----------------------------------------------------------------------
364 // if no native match they just return the passed-in id
372 IdPair gCommandIds
[] =
374 { kHICommandCut
, wxID_CUT
} ,
375 { kHICommandCopy
, wxID_COPY
} ,
376 { kHICommandPaste
, wxID_PASTE
} ,
377 { kHICommandSelectAll
, wxID_SELECTALL
} ,
378 { kHICommandClear
, wxID_CLEAR
} ,
379 { kHICommandUndo
, wxID_UNDO
} ,
380 { kHICommandRedo
, wxID_REDO
} ,
383 int wxMacCommandToId( UInt32 macCommandId
)
387 switch ( macCommandId
)
389 case kHICommandPreferences
:
390 wxid
= wxApp::s_macPreferencesMenuItemId
;
393 case kHICommandQuit
:
394 wxid
= wxApp::s_macExitMenuItemId
;
397 case kHICommandAbout
:
398 wxid
= wxApp::s_macAboutMenuItemId
;
403 for ( size_t i
= 0 ; i
< WXSIZEOF(gCommandIds
) ; ++i
)
405 if ( gCommandIds
[i
].macId
== macCommandId
)
407 wxid
= gCommandIds
[i
].wxId
;
416 wxid
= (int) macCommandId
;
421 UInt32
wxIdToMacCommand( int wxId
)
425 if ( wxId
== wxApp::s_macPreferencesMenuItemId
)
426 macId
= kHICommandPreferences
;
427 else if (wxId
== wxApp::s_macExitMenuItemId
)
428 macId
= kHICommandQuit
;
429 else if (wxId
== wxApp::s_macAboutMenuItemId
)
430 macId
= kHICommandAbout
;
433 for ( size_t i
= 0 ; i
< WXSIZEOF(gCommandIds
) ; ++i
)
435 if ( gCommandIds
[i
].wxId
== wxId
)
437 macId
= gCommandIds
[i
].macId
;
449 wxMenu
* wxFindMenuFromMacCommand( const HICommand
&command
, wxMenuItem
* &item
)
451 wxMenu
* itemMenu
= NULL
;
454 // for 'standard' commands which don't have a wx-menu
455 if ( command
.commandID
== kHICommandPreferences
|| command
.commandID
== kHICommandQuit
|| command
.commandID
== kHICommandAbout
)
457 id
= wxMacCommandToId( command
.commandID
) ;
459 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
461 item
= mbar
->FindItem( id
, &itemMenu
) ;
463 else if ( command
.commandID
!= 0 && command
.menu
.menuRef
!= 0 && command
.menu
.menuItemIndex
!= 0 )
465 id
= wxMacCommandToId( command
.commandID
) ;
466 // make sure it is one of our own menus, or of the 'synthetic' apple and help menus , otherwise don't touch
467 MenuItemIndex firstUserHelpMenuItem
;
468 static MenuHandle mh
= NULL
;
471 if ( UMAGetHelpMenu( &mh
, &firstUserHelpMenuItem
) != noErr
)
475 // is it part of the application or the Help menu, then look for the id directly
476 if ( ( GetMenuHandle( kwxMacAppleMenuId
) != NULL
&& command
.menu
.menuRef
== GetMenuHandle( kwxMacAppleMenuId
) ) ||
477 ( mh
!= NULL
&& command
.menu
.menuRef
== mh
) )
479 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
481 item
= mbar
->FindItem( id
, &itemMenu
) ;
487 GetMenuItemRefCon( command
.menu
.menuRef
, command
.menu
.menuItemIndex
, &refCon
) ;
488 itemMenu
= wxFindMenuFromMacMenu( command
.menu
.menuRef
) ;
489 if ( itemMenu
!= NULL
)
490 item
= (wxMenuItem
*) refCon
;
497 //----------------------------------------------------------------------
498 // Carbon Event Handler
499 //----------------------------------------------------------------------
501 static const EventTypeSpec eventList
[] =
503 { kEventClassCommand
, kEventProcessCommand
} ,
504 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
506 { kEventClassMenu
, kEventMenuOpening
},
507 { kEventClassMenu
, kEventMenuClosed
},
508 { kEventClassMenu
, kEventMenuTargetItem
},
510 { kEventClassApplication
, kEventAppActivated
} ,
511 { kEventClassApplication
, kEventAppDeactivated
} ,
512 // handling the quit event is not recommended by apple
513 // rather using the quit apple event - which we do
515 { kEventClassAppleEvent
, kEventAppleEvent
} ,
517 { kEventClassMouse
, kEventMouseDown
} ,
518 { kEventClassMouse
, kEventMouseMoved
} ,
519 { kEventClassMouse
, kEventMouseUp
} ,
520 { kEventClassMouse
, kEventMouseDragged
} ,
524 static pascal OSStatus
525 wxMacAppMenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
527 wxMacCarbonEvent
cEvent( event
) ;
528 MenuRef menuRef
= cEvent
.GetParameter
<MenuRef
>(kEventParamDirectObject
) ;
529 wxMenu
* menu
= wxFindMenuFromMacMenu( menuRef
) ;
535 switch (GetEventKind(event
))
537 case kEventMenuOpening
:
538 type
= wxEVT_MENU_OPEN
;
541 case kEventMenuClosed
:
542 type
= wxEVT_MENU_CLOSE
;
545 case kEventMenuTargetItem
:
546 cmd
= cEvent
.GetParameter
<MenuCommand
>(kEventParamMenuCommand
,typeMenuCommand
) ;
548 type
= wxEVT_MENU_HIGHLIGHT
;
552 wxFAIL_MSG(wxT("Unexpected menu event kind"));
558 wxMenuEvent
wxevent(type
, cmd
, menu
);
559 wxevent
.SetEventObject(menu
);
561 wxEvtHandler
* handler
= menu
->GetEventHandler();
562 if (handler
&& handler
->ProcessEvent(wxevent
))
568 wxWindow
*win
= menu
->GetInvokingWindow();
570 win
->GetEventHandler()->ProcessEvent(wxevent
);
575 return eventNotHandledErr
;
578 static pascal OSStatus
wxMacAppCommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
580 OSStatus result
= eventNotHandledErr
;
584 wxMacCarbonEvent
cEvent( event
) ;
585 cEvent
.GetParameter
<HICommand
>(kEventParamDirectObject
,typeHICommand
,&command
) ;
587 wxMenuItem
* item
= NULL
;
588 wxMenu
* itemMenu
= wxFindMenuFromMacCommand( command
, item
) ;
589 int id
= wxMacCommandToId( command
.commandID
) ;
593 wxASSERT( itemMenu
!= NULL
) ;
595 switch ( cEvent
.GetKind() )
597 case kEventProcessCommand
:
599 if (item
->IsCheckable())
600 item
->Check( !item
->IsChecked() ) ;
602 if ( itemMenu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) )
607 case kEventCommandUpdateStatus
:
609 wxUpdateUIEvent
event(id
);
610 event
.SetEventObject( itemMenu
);
612 bool processed
= false;
614 // Try the menu's event handler
616 wxEvtHandler
*handler
= itemMenu
->GetEventHandler();
618 processed
= handler
->ProcessEvent(event
);
621 // Try the window the menu was popped up from
622 // (and up through the hierarchy)
625 const wxMenuBase
*menu
= itemMenu
;
628 wxWindow
*win
= menu
->GetInvokingWindow();
631 processed
= win
->GetEventHandler()->ProcessEvent(event
);
635 menu
= menu
->GetParent();
641 // if anything changed, update the changed attribute
642 if (event
.GetSetText())
643 itemMenu
->SetLabel(id
, event
.GetText());
644 if (event
.GetSetChecked())
645 itemMenu
->Check(id
, event
.GetChecked());
646 if (event
.GetSetEnabled())
647 itemMenu
->Enable(id
, event
.GetEnabled());
661 static pascal OSStatus
wxMacAppApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
663 OSStatus result
= eventNotHandledErr
;
664 switch ( GetEventKind( event
) )
666 case kEventAppActivated
:
668 wxTheApp
->SetActive( true , NULL
) ;
672 case kEventAppDeactivated
:
674 wxTheApp
->SetActive( false , NULL
) ;
685 pascal OSStatus
wxMacAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
687 EventRef formerEvent
= (EventRef
) wxTheApp
->MacGetCurrentEvent() ;
688 EventHandlerCallRef formerEventHandlerCallRef
= (EventHandlerCallRef
) wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
689 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
691 OSStatus result
= eventNotHandledErr
;
692 switch ( GetEventClass( event
) )
694 case kEventClassCommand
:
695 result
= wxMacAppCommandEventHandler( handler
, event
, data
) ;
698 case kEventClassApplication
:
699 result
= wxMacAppApplicationEventHandler( handler
, event
, data
) ;
702 case kEventClassMenu
:
703 result
= wxMacAppMenuEventHandler( handler
, event
, data
) ;
706 case kEventClassMouse
:
708 wxMacCarbonEvent
cEvent( event
) ;
711 Point screenMouseLocation
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ;
712 ::FindWindow(screenMouseLocation
, &window
);
713 // only send this event in case it had not already been sent to a tlw, as we get
714 // double events otherwise (in case event.skip) was called
715 if ( window
== NULL
)
716 result
= wxMacTopLevelMouseEventHandler( handler
, event
, NULL
) ;
720 case kEventClassAppleEvent
:
724 wxMacConvertEventToRecord( event
, &rec
) ;
725 result
= AEProcessAppleEvent( &rec
) ;
733 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerEventHandlerCallRef
) ;
738 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler
)
740 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
741 // we know it's there ;-)
742 WXIMPORT
char std::__throws_bad_alloc
;
747 pascal static void wxMacAssertOutputHandler(OSType componentSignature
, UInt32 options
,
748 const char *assertionString
, const char *exceptionLabelString
,
749 const char *errorString
, const char *fileName
, long lineNumber
, void *value
, ConstStr255Param outputMsg
)
751 // flow into assert handling
752 wxString fileNameStr
;
753 wxString assertionStr
;
754 wxString exceptionStr
;
758 fileNameStr
= wxString(fileName
, wxConvLocal
);
759 assertionStr
= wxString(assertionString
, wxConvLocal
);
760 exceptionStr
= wxString((exceptionLabelString
!=0) ? exceptionLabelString
: "", wxConvLocal
) ;
761 errorStr
= wxString((errorString
!=0) ? errorString
: "", wxConvLocal
) ;
763 fileNameStr
= fileName
;
764 assertionStr
= assertionString
;
765 exceptionStr
= (exceptionLabelString
!=0) ? exceptionLabelString
: "" ;
766 errorStr
= (errorString
!=0) ? errorString
: "" ;
771 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
772 assertionStr
.c_str() ,
773 exceptionStr
.c_str() ,
775 fileNameStr
.c_str(), lineNumber
,
779 wxOnAssert(fileNameStr
, lineNumber
, assertionStr
,
780 wxString::Format( wxT("%s %s value (%p)") , exceptionStr
, errorStr
, value
) ) ;
789 // m_macEventPosted run loop source callback:
790 void macPostedEventCallback(void *unused
);
793 void macPostedEventCallback(void *unused
)
795 wxTheApp
->ProcessPendingEvents();
799 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
804 InstallDebugAssertOutputHandler( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler
) );
807 UMAInitToolbox( 4, sm_isEmbedded
) ;
808 SetEventMask( everyEvent
) ;
809 UMAShowWatchCursor() ;
812 # if __option(profile)
813 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
818 // now avoid exceptions thrown for new (bad_alloc)
819 // FIXME CS for some changes outside wxMac does not compile anymore
821 std::__throws_bad_alloc
= 0 ;
826 s_macCursorRgn
= ::NewRgn() ;
828 // Mac OS X passes a process serial number command line argument when
829 // the application is launched from the Finder. This argument must be
830 // removed from the command line arguments before being handled by the
831 // application (otherwise applications would need to handle it)
834 static const wxChar
*ARG_PSN
= _T("-psn_");
835 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
837 // remove this argument
839 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
843 if ( !wxAppBase::Initialize(argc
, argv
) )
847 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
850 #if TARGET_API_MAC_OSX
851 // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
852 wxString startupCwd
= wxGetCwd() ;
853 if ( startupCwd
== wxT("/") || startupCwd
.Right(15) == wxT("/Contents/MacOS") )
855 CFURLRef url
= CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
856 CFURLRef urlParent
= CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault
, url
) ;
858 CFStringRef path
= CFURLCopyFileSystemPath ( urlParent
, kCFURLPOSIXPathStyle
) ;
859 CFRelease( urlParent
) ;
860 wxString cwd
= wxMacCFStringHolder(path
).AsString(wxLocale::GetSystemEncoding());
861 wxSetWorkingDirectory( cwd
) ;
865 wxMacCreateNotifierTable() ;
868 /* connect posted events to common-mode run loop so that wxPostEvent events
869 are handled even while we're in the menu or on a scrollbar */
870 CFRunLoopSourceContext event_posted_context
= {0};
871 event_posted_context
.perform
= macPostedEventCallback
;
872 m_macEventPosted
= CFRunLoopSourceCreate(NULL
,0,&event_posted_context
);
873 CFRunLoopAddSource(CFRunLoopGetCurrent(), m_macEventPosted
, kCFRunLoopCommonModes
);
876 UMAShowArrowCursor() ;
881 AEEventHandlerUPP sODocHandler
= NULL
;
882 AEEventHandlerUPP sOAppHandler
= NULL
;
883 AEEventHandlerUPP sPDocHandler
= NULL
;
884 AEEventHandlerUPP sRAppHandler
= NULL
;
885 AEEventHandlerUPP sQuitHandler
= NULL
;
887 bool wxApp::OnInitGui()
889 if ( !wxAppBase::OnInitGui() )
892 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
896 InstallApplicationEventHandler(
897 GetwxMacAppEventHandlerUPP(),
898 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
903 sODocHandler
= NewAEEventHandlerUPP(AEHandleODoc
) ;
904 sOAppHandler
= NewAEEventHandlerUPP(AEHandleOApp
) ;
905 sPDocHandler
= NewAEEventHandlerUPP(AEHandlePDoc
) ;
906 sRAppHandler
= NewAEEventHandlerUPP(AEHandleRApp
) ;
907 sQuitHandler
= NewAEEventHandlerUPP(AEHandleQuit
) ;
909 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
910 sODocHandler
, 0 , FALSE
) ;
911 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
912 sOAppHandler
, 0 , FALSE
) ;
913 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
914 sPDocHandler
, 0 , FALSE
) ;
915 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
916 sRAppHandler
, 0 , FALSE
) ;
917 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
918 sQuitHandler
, 0 , FALSE
) ;
924 void wxApp::CleanUp()
927 wxToolTip::RemoveToolTips() ;
931 if (m_macEventPosted
)
932 CFRelease(m_macEventPosted
);
933 m_macEventPosted
= NULL
;
936 // One last chance for pending objects to be cleaned up
937 wxTheApp
->DeletePendingObjects();
939 wxMacDestroyNotifierTable() ;
942 # if __option(profile)
943 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
948 UMACleanupToolbox() ;
950 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
953 RemoveEventHandler( (EventHandlerRef
)(wxTheApp
->m_macEventHandler
) );
957 AERemoveEventHandler( kCoreEventClass
, kAEOpenDocuments
,
958 sODocHandler
, FALSE
) ;
959 AERemoveEventHandler( kCoreEventClass
, kAEOpenApplication
,
960 sOAppHandler
, FALSE
) ;
961 AERemoveEventHandler( kCoreEventClass
, kAEPrintDocuments
,
962 sPDocHandler
, FALSE
) ;
963 AERemoveEventHandler( kCoreEventClass
, kAEReopenApplication
,
964 sRAppHandler
, FALSE
) ;
965 AERemoveEventHandler( kCoreEventClass
, kAEQuitApplication
,
966 sQuitHandler
, FALSE
) ;
968 DisposeAEEventHandlerUPP( sODocHandler
) ;
969 DisposeAEEventHandlerUPP( sOAppHandler
) ;
970 DisposeAEEventHandlerUPP( sPDocHandler
) ;
971 DisposeAEEventHandlerUPP( sRAppHandler
) ;
972 DisposeAEEventHandlerUPP( sQuitHandler
) ;
975 wxAppBase::CleanUp();
978 //----------------------------------------------------------------------
979 // misc initialization stuff
980 //----------------------------------------------------------------------
982 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
984 // for shared libraries we have to manually get the correct resource
985 // ref num upon initializing and releasing when terminating, therefore
986 // the __wxinitialize and __wxterminate must be used
990 void __sinit(void); // (generated by linker)
991 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
992 pascal void __terminate(void);
995 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
997 return __initialize( theInitBlock
) ;
1000 pascal void __wxterminate(void)
1005 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
1007 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
1009 OSStatus err
= noErr
;
1010 bool converted
= ConvertEventRefToEventRecord( event
, rec
) ;
1014 switch ( GetEventClass( event
) )
1016 case kEventClassKeyboard
:
1019 switch ( GetEventKind(event
) )
1021 case kEventRawKeyDown
:
1022 rec
->what
= keyDown
;
1025 case kEventRawKeyRepeat
:
1026 rec
->what
= autoKey
;
1029 case kEventRawKeyUp
:
1033 case kEventRawKeyModifiersChanged
:
1034 rec
->what
= nullEvent
;
1045 unsigned char charCode
;
1047 GetMouse( &rec
->where
) ;
1049 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1050 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1051 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1052 rec
->modifiers
= modifiers
;
1053 rec
->message
= (keyCode
<< 8 ) + charCode
;
1058 case kEventClassTextInput
:
1060 switch ( GetEventKind( event
) )
1062 case kEventTextInputUnicodeForKeyEvent
:
1065 err
= GetEventParameter(
1066 event
, kEventParamTextInputSendKeyboardEvent
, typeEventRef
, NULL
,
1067 sizeof(rawEvent
), NULL
, &rawEvent
) ;
1071 UInt32 keyCode
, modifiers
;
1072 unsigned char charCode
;
1074 GetMouse( &rec
->where
) ;
1075 rec
->what
= keyDown
;
1076 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
1077 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
1078 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
1079 rec
->modifiers
= modifiers
;
1080 rec
->message
= (keyCode
<< 8 ) + charCode
;
1101 m_printMode
= wxPRINT_WINDOWS
;
1103 m_macCurrentEvent
= NULL
;
1104 m_macCurrentEventHandlerCallRef
= NULL
;
1106 #ifdef __WXMAC_OSX__
1107 m_macEventPosted
= NULL
;
1111 int wxApp::MainLoop()
1116 RunApplicationEventLoop() ;
1127 void wxApp::ExitMainLoop()
1129 m_keepGoing
= false;
1132 QuitApplicationEventLoop() ;
1136 // Is a message/event pending?
1137 bool wxApp::Pending()
1139 // without the receive event (with pull param = false ) nothing is ever reported
1142 ReceiveNextEvent(0, NULL
, kEventDurationNoWait
, false, &theEvent
);
1144 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1147 // Dispatch a message.
1148 bool wxApp::Dispatch()
1155 void wxApp::OnIdle(wxIdleEvent
& event
)
1157 wxAppBase::OnIdle(event
);
1159 // If they are pending events, we must process them: pending events are
1160 // either events to the threads other than main or events posted with
1161 // wxPostEvent() functions
1162 wxMacProcessNotifierAndPendingEvents();
1164 if (!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1165 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1168 void wxApp::WakeUpIdle()
1170 #ifdef __WXMAC_OSX__
1171 if (m_macEventPosted
)
1173 CFRunLoopSourceSignal(m_macEventPosted
);
1186 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1189 GetTopWindow()->Close(true);
1192 // Default behaviour: close the application with prompts. The
1193 // user can veto the close, and therefore the end session.
1194 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1198 if (!GetTopWindow()->Close(!event
.CanVeto()))
1203 extern "C" void wxCYield() ;
1209 // Yield to other processes
1211 bool wxApp::Yield(bool onlyIfNeeded
)
1215 if ( !onlyIfNeeded
)
1217 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1225 // by definition yield should handle all non-processed events
1229 OSStatus status
= noErr
;
1231 while ( status
== noErr
)
1233 s_inReceiveEvent
= true ;
1234 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1235 s_inReceiveEvent
= false ;
1237 if ( status
== eventLoopTimedOutErr
)
1239 // make sure next time the event loop will trigger idle events
1240 sleepTime
= kEventDurationNoWait
;
1242 else if ( status
== eventLoopQuitErr
)
1244 // according to QA1061 this may also occur when a WakeUp Process
1249 MacHandleOneEvent( theEvent
) ;
1250 ReleaseEvent(theEvent
);
1254 wxMacProcessNotifierAndPendingEvents() ;
1260 void wxApp::MacDoOneEvent()
1264 s_inReceiveEvent
= true ;
1265 OSStatus status
= ReceiveNextEvent(0, NULL
, sleepTime
, true, &theEvent
) ;
1266 s_inReceiveEvent
= false ;
1270 case eventLoopTimedOutErr
:
1271 if ( wxTheApp
->ProcessIdle() )
1272 sleepTime
= kEventDurationNoWait
;
1274 sleepTime
= kEventDurationSecond
;
1277 case eventLoopQuitErr
:
1278 // according to QA1061 this may also occur
1279 // when a WakeUp Process is executed
1283 MacHandleOneEvent( theEvent
) ;
1284 ReleaseEvent( theEvent
);
1285 sleepTime
= kEventDurationNoWait
;
1290 DeletePendingObjects() ;
1291 wxMacProcessNotifierAndPendingEvents() ;
1295 void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1297 // Override to process unhandled events as you please
1300 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1302 EventTargetRef theTarget
;
1303 theTarget
= GetEventDispatcherTarget();
1304 m_macCurrentEvent
= evr
;
1306 OSStatus status
= SendEventToEventTarget((EventRef
) evr
, theTarget
);
1307 if (status
== eventNotHandledErr
)
1308 MacHandleUnhandledEvent(evr
);
1310 wxMacProcessNotifierAndPendingEvents() ;
1313 wxMutexGuiLeaveOrEnter();
1314 #endif // wxUSE_THREADS
1317 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1318 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1323 case kHomeCharCode
:
1327 case kEnterCharCode
:
1328 retval
= WXK_RETURN
;
1334 case kHelpCharCode
:
1338 case kBackspaceCharCode
:
1346 case kPageUpCharCode
:
1347 retval
= WXK_PAGEUP
;
1350 case kPageDownCharCode
:
1351 retval
= WXK_PAGEDOWN
;
1354 case kReturnCharCode
:
1355 retval
= WXK_RETURN
;
1358 case kFunctionKeyCharCode
:
1428 case kEscapeCharCode
:
1429 retval
= WXK_ESCAPE
;
1432 case kLeftArrowCharCode
:
1436 case kRightArrowCharCode
:
1437 retval
= WXK_RIGHT
;
1440 case kUpArrowCharCode
:
1444 case kDownArrowCharCode
:
1448 case kDeleteCharCode
:
1449 retval
= WXK_DELETE
;
1459 int wxMacKeyCodeToModifier(wxKeyCode key
)
1485 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1487 wxASSERT_MSG(key
!= WXK_LBUTTON
&& key
!= WXK_RBUTTON
&& key
!=
1488 WXK_MBUTTON
, wxT("can't use wxGetKeyState() for mouse buttons"));
1490 //if OS X > 10.2 (i.e. 10.2.x)
1491 //a known apple bug prevents the system from determining led
1492 //states with GetKeys... can only determine caps lock led
1493 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key
));
1495 // KeyMapByteArray keymap;
1496 // GetKeys((BigEndianLong*)keymap);
1497 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1502 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1509 keychar
= short(keymessage
& charCodeMask
);
1510 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1512 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1514 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1515 // and look at the character after
1517 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1518 keychar
= short(keyInfo
& charCodeMask
);
1521 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1522 long realkeyval
= keyval
;
1523 if ( keyval
== keychar
)
1525 // we are not on a special character combo -> pass the real os event-value to EVT_CHAR, but not to EVT_KEY (make upper first)
1526 realkeyval
= short(keymessage
& charCodeMask
) ;
1527 keyval
= wxToupper( keyval
) ;
1530 // Check for NUMPAD keys
1531 if (keyval
>= '0' && keyval
<= '9' && keycode
>= 82 && keycode
<= 92)
1533 keyval
= keyval
- '0' + WXK_NUMPAD0
;
1535 else if (keycode
>= 67 && keycode
<= 81)
1540 keyval
= WXK_NUMPAD_ENTER
;
1544 keyval
= WXK_NUMPAD_EQUAL
;
1548 keyval
= WXK_NUMPAD_MULTIPLY
;
1552 keyval
= WXK_NUMPAD_DIVIDE
;
1556 keyval
= WXK_NUMPAD_SUBTRACT
;
1560 keyval
= WXK_NUMPAD_ADD
;
1564 keyval
= WXK_NUMPAD_DECIMAL
;
1572 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1573 bool handled
= false ;
1574 event
.m_shiftDown
= modifiers
& shiftKey
;
1575 event
.m_controlDown
= modifiers
& controlKey
;
1576 event
.m_altDown
= modifiers
& optionKey
;
1577 event
.m_metaDown
= modifiers
& cmdKey
;
1578 event
.m_keyCode
= keyval
;
1581 event
.m_uniChar
= uniChar
;
1584 event
.m_rawCode
= keymessage
;
1585 event
.m_rawFlags
= modifiers
;
1588 event
.SetTimestamp(when
);
1589 event
.SetEventObject(focus
);
1590 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1591 if ( handled
&& event
.GetSkipped() )
1597 wxWindow
*ancestor
= focus
;
1600 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1603 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1604 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1608 if (ancestor
->IsTopLevel())
1611 ancestor
= ancestor
->GetParent();
1614 #endif // wxUSE_ACCEL
1618 wxTopLevelWindowMac
*tlw
= focus
->MacGetTopLevelWindow() ;
1622 event
.Skip( false ) ;
1623 event
.SetEventType( wxEVT_CHAR_HOOK
);
1625 event
.m_keyCode
= realkeyval
;
1627 handled
= tlw
->GetEventHandler()->ProcessEvent( event
);
1628 if ( handled
&& event
.GetSkipped() )
1635 event
.Skip( false ) ;
1636 event
.SetEventType( wxEVT_CHAR
) ;
1639 event
.m_keyCode
= realkeyval
;
1641 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1642 if ( handled
&& event
.GetSkipped() )
1646 if ( !handled
&& (keyval
== WXK_TAB
) )
1648 wxWindow
* iter
= focus
->GetParent() ;
1649 while ( iter
&& !handled
)
1651 if ( iter
->HasFlag( wxTAB_TRAVERSAL
) )
1653 wxNavigationKeyEvent new_event
;
1654 new_event
.SetEventObject( focus
);
1655 new_event
.SetDirection( !event
.ShiftDown() );
1656 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1657 new_event
.SetWindowChange( event
.ControlDown() );
1658 new_event
.SetCurrentFocus( focus
);
1659 handled
= focus
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
1660 if ( handled
&& new_event
.GetSkipped() )
1664 iter
= iter
->GetParent() ;
1668 // backdoor handler for default return and command escape
1669 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1671 // if window is not having a focus still testing for default enter or cancel
1672 // TODO: add the UMA version for ActiveNonFloatingWindow
1673 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1676 if ( keyval
== WXK_RETURN
)
1678 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(), wxButton
);
1679 if ( def
&& def
->IsEnabled() )
1681 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1682 event
.SetEventObject(def
);
1683 def
->Command(event
);
1688 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1690 // generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs)
1691 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1692 new_event
.SetEventObject( focus
);
1693 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1701 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1706 short keycode
, keychar
;
1708 keychar
= short(keymessage
& charCodeMask
);
1709 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1710 if ( modifiers
& (controlKey
| shiftKey
| optionKey
) )
1712 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1713 // and look at the character after
1715 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
| shiftKey
| optionKey
))) | keycode
, &state
);
1716 keychar
= short(keyInfo
& charCodeMask
);
1719 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1720 if ( keyval
== keychar
)
1721 keyval
= wxToupper( keyval
) ;
1723 // Check for NUMPAD keys
1724 if (keyval
>= '0' && keyval
<= '9' && keycode
>= 82 && keycode
<= 92)
1726 keyval
= (keyval
- '0') + WXK_NUMPAD0
;
1728 else if (keycode
>= 67 && keycode
<= 81)
1733 keyval
= WXK_NUMPAD_ENTER
;
1737 keyval
= WXK_NUMPAD_EQUAL
;
1741 keyval
= WXK_NUMPAD_MULTIPLY
;
1745 keyval
= WXK_NUMPAD_DIVIDE
;
1749 keyval
= WXK_NUMPAD_SUBTRACT
;
1753 keyval
= WXK_NUMPAD_ADD
;
1757 keyval
= WXK_NUMPAD_DECIMAL
;
1765 bool handled
= false ;
1767 wxKeyEvent
event(wxEVT_KEY_UP
);
1768 event
.m_shiftDown
= modifiers
& shiftKey
;
1769 event
.m_controlDown
= modifiers
& controlKey
;
1770 event
.m_altDown
= modifiers
& optionKey
;
1771 event
.m_metaDown
= modifiers
& cmdKey
;
1772 event
.m_keyCode
= keyval
;
1774 event
.m_uniChar
= uniChar
;
1777 event
.m_rawCode
= keymessage
;
1778 event
.m_rawFlags
= modifiers
;
1781 event
.SetTimestamp(when
);
1782 event
.SetEventObject(focus
);
1783 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;