1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "app.h"
16 #include "wx/wxprec.h"
18 #include "wx/window.h"
20 #include "wx/button.h"
23 #include "wx/gdicmn.h"
26 #include "wx/cursor.h"
29 #include "wx/palette.h"
31 #include "wx/dialog.h"
32 #include "wx/msgdlg.h"
34 #include "wx/module.h"
35 #include "wx/memory.h"
36 #include "wx/tooltip.h"
37 #include "wx/textctrl.h"
39 #include "wx/docview.h"
40 #include "wx/filename.h"
52 // #include "apprsrc.h"
54 #include "wx/mac/uma.h"
55 #include "wx/mac/macnotfy.h"
58 # include <CoreServices/CoreServices.h>
59 # if defined(WXMAKINGDLL_CORE)
60 # include <mach-o/dyld.h>
65 # include <ToolUtils.h>
66 # include <DiskInit.h>
70 extern wxList wxPendingDelete
;
72 // set wxMAC_USE_RAEL to 1 if RunApplicationEventLoop should be used
73 // if 0 the lower level CarbonEventLoop will be used
74 // on the long run RAEL should replace the low level event loop
75 // we will have to clean up event handling to make sure we don't
76 // miss handling of things like pending events etc
77 // perhaps we will also have to pipe events through an ueber-event-handler
78 // to make sure we have one place to do all these house-keeping functions
80 #define wxMAC_USE_RAEL 0
83 extern size_t g_numberOfThreads
;
84 #endif // wxUSE_THREADS
86 // statics for implementation
88 static bool s_inYield
= FALSE
;
90 static bool s_inReceiveEvent
= FALSE
;
91 static EventTime sleepTime
= kEventDurationNoWait
;
93 #if !USE_SHARED_LIBRARY
94 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
95 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
96 EVT_IDLE(wxApp::OnIdle
)
97 EVT_END_SESSION(wxApp::OnEndSession
)
98 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
103 const short kMacMinHeap
= (29 * 1024) ;
104 // platform specific static variables
106 const short kwxMacMenuBarResource
= 1 ;
107 const short kwxMacAppleMenuId
= 1 ;
109 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
110 wxWindow
* wxApp::s_captureWindow
= NULL
;
111 int wxApp::s_lastMouseDown
= 0 ;
112 long wxApp::sm_lastMessageTime
= 0;
113 long wxApp::s_lastModifiers
= 0 ;
116 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
117 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
118 long wxApp::s_macPreferencesMenuItemId
= wxID_PREFERENCES
;
119 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
120 wxString
wxApp::s_macHelpMenuTitleName
= wxT("&Help") ;
122 // Normally we're not a plugin
123 bool wxApp::sm_isEmbedded
= false;
124 //----------------------------------------------------------------------
125 // Core Apple Event Support
126 //----------------------------------------------------------------------
128 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
129 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
130 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
131 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
132 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
134 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
136 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
139 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
141 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
144 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
146 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
149 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
151 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
154 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
156 return wxTheApp
->MacHandleAERApp( (AppleEvent
*) event
, reply
) ;
159 // AEODoc Calls MacOpenFile on each of the files passed
161 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
165 DescType returnedType
;
170 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
174 err
= AECountItems(&docList
, &itemsInList
);
178 ProcessSerialNumber PSN
;
179 PSN
.highLongOfPSN
= 0 ;
180 PSN
.lowLongOfPSN
= kCurrentProcess
;
181 SetFrontProcess( &PSN
) ;
183 for (i
= 1; i
<= itemsInList
; i
++)
188 AEGetNthPtr(&docList
, i
, typeFSRef
, &keywd
, &returnedType
,
189 (Ptr
) & theRef
, sizeof(theRef
), &actualSize
);
190 fName
= wxMacFSRefToPath( &theRef
) ;
197 // AEPDoc Calls MacPrintFile on each of the files passed
199 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
203 DescType returnedType
;
208 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
212 err
= AECountItems(&docList
, &itemsInList
);
216 ProcessSerialNumber PSN
;
217 PSN
.highLongOfPSN
= 0 ;
218 PSN
.lowLongOfPSN
= kCurrentProcess
;
219 SetFrontProcess( &PSN
) ;
221 for (i
= 1; i
<= itemsInList
; i
++) {
225 AEGetNthPtr(&docList
, i
, typeFSRef
, &keywd
, &returnedType
,
226 (Ptr
) & theRef
, sizeof(theRef
), &actualSize
);
227 fName
= wxMacFSRefToPath( &theRef
) ;
234 // AEOApp calls MacNewFile
236 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
242 // AEQuit attempts to quit the application
244 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
246 wxWindow
* win
= GetTopWindow() ;
249 wxCommandEvent
exitEvent(wxEVT_COMMAND_MENU_SELECTED
, s_macExitMenuItemId
);
250 if (!win
->ProcessEvent(exitEvent
))
260 // AEROApp calls MacReopenApp
262 short wxApp::MacHandleAERApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
270 //----------------------------------------------------------------------
271 // Support Routines linking the Mac...File Calls to the Document Manager
272 //----------------------------------------------------------------------
274 void wxApp::MacOpenFile(const wxString
& fileName
)
276 #if wxUSE_DOC_VIEW_ARCHITECTURE
277 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
279 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
284 void wxApp::MacPrintFile(const wxString
& fileName
)
286 #if wxUSE_DOC_VIEW_ARCHITECTURE
288 #if wxUSE_PRINTING_ARCHITECTURE
289 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
292 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
295 wxView
* view
= doc
->GetFirstView() ;
298 wxPrintout
*printout
= view
->OnCreatePrintout();
302 printer
.Print(view
->GetFrame(), printout
, TRUE
);
308 doc
->DeleteAllViews();
309 dm
->RemoveDocument(doc
) ;
320 void wxApp::MacNewFile()
324 void wxApp::MacReopenApp()
327 // if there is no open window -> create a new one
328 // if all windows are hidden -> show the first
329 // if some windows are not hidden -> do nothing
331 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
338 wxTopLevelWindow
* firstIconized
= NULL
;
341 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->GetData();
342 if ( win
->IsIconized() == false )
344 firstIconized
= NULL
;
349 if ( firstIconized
== NULL
)
350 firstIconized
= win
;
352 node
= node
->GetNext();
355 firstIconized
->Iconize( false ) ;
359 //----------------------------------------------------------------------
360 // Carbon Event Handler
361 //----------------------------------------------------------------------
363 static const EventTypeSpec eventList
[] =
365 { kEventClassCommand
, kEventProcessCommand
} ,
366 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
368 { kEventClassMenu
, kEventMenuOpening
},
369 { kEventClassMenu
, kEventMenuClosed
},
370 { kEventClassMenu
, kEventMenuTargetItem
},
372 { kEventClassApplication
, kEventAppActivated
} ,
373 { kEventClassApplication
, kEventAppDeactivated
} ,
374 // handling the quit event is not recommended by apple
375 // rather using the quit apple event - which we do
377 { kEventClassAppleEvent
, kEventAppleEvent
} ,
379 { kEventClassMouse
, kEventMouseDown
} ,
380 { kEventClassMouse
, kEventMouseMoved
} ,
381 { kEventClassMouse
, kEventMouseUp
} ,
382 { kEventClassMouse
, kEventMouseDragged
} ,
386 static pascal OSStatus
387 wxMacAppMenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
389 wxMacCarbonEvent
cEvent( event
) ;
390 MenuRef menuRef
= cEvent
.GetParameter
<MenuRef
>(kEventParamDirectObject
) ;
391 wxMenu
* menu
= wxFindMenuFromMacMenu( menuRef
) ;
397 switch (GetEventKind(event
))
399 case kEventMenuOpening
:
400 type
= wxEVT_MENU_OPEN
;
402 case kEventMenuClosed
:
403 type
= wxEVT_MENU_CLOSE
;
405 case kEventMenuTargetItem
:
406 cmd
= cEvent
.GetParameter
<MenuCommand
>(kEventParamMenuCommand
,typeMenuCommand
) ;
408 type
= wxEVT_MENU_HIGHLIGHT
;
411 wxFAIL_MSG(wxT("Unexpected menu event kind"));
417 wxMenuEvent
wxevent(type
, cmd
, menu
);
418 wxevent
.SetEventObject(menu
);
420 wxEvtHandler
* handler
= menu
->GetEventHandler();
421 if (handler
&& handler
->ProcessEvent(wxevent
))
427 wxWindow
*win
= menu
->GetInvokingWindow();
429 win
->GetEventHandler()->ProcessEvent(wxevent
);
434 return eventNotHandledErr
;
437 static pascal OSStatus
wxMacAppCommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
439 OSStatus result
= eventNotHandledErr
;
443 wxMacCarbonEvent
cEvent( event
) ;
444 cEvent
.GetParameter
<HICommand
>(kEventParamDirectObject
,typeHICommand
,&command
) ;
446 wxMenuItem
* item
= NULL
;
447 MenuCommand id
= command
.commandID
;
448 // for items we don't really control
449 if ( id
== kHICommandPreferences
)
451 id
= wxApp::s_macPreferencesMenuItemId
;
453 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
456 wxMenu
* menu
= NULL
;
457 item
= mbar
->FindItem( id
, &menu
) ;
460 else if ( id
!= 0 && command
.menu
.menuRef
!= 0 && command
.menu
.menuItemIndex
!= 0 )
462 GetMenuItemRefCon( command
.menu
.menuRef
, command
.menu
.menuItemIndex
, (UInt32
*) &item
) ;
467 switch( cEvent
.GetKind() )
469 case kEventProcessCommand
:
471 if (item
->IsCheckable())
473 item
->Check( !item
->IsChecked() ) ;
476 item
->GetMenu()->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
480 case kEventCommandUpdateStatus
:
481 // eventually trigger an updateui round
491 static pascal OSStatus
wxMacAppApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
493 OSStatus result
= eventNotHandledErr
;
494 switch ( GetEventKind( event
) )
496 case kEventAppActivated
:
499 wxTheApp
->SetActive( true , NULL
) ;
503 case kEventAppDeactivated
:
506 wxTheApp
->SetActive( false , NULL
) ;
516 pascal OSStatus
wxMacAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
518 EventRef formerEvent
= (EventRef
) wxTheApp
->MacGetCurrentEvent() ;
519 EventHandlerCallRef formerEventHandlerCallRef
= (EventHandlerCallRef
) wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
520 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
522 OSStatus result
= eventNotHandledErr
;
523 switch( GetEventClass( event
) )
525 case kEventClassCommand
:
526 result
= wxMacAppCommandEventHandler( handler
, event
, data
) ;
528 case kEventClassApplication
:
529 result
= wxMacAppApplicationEventHandler( handler
, event
, data
) ;
531 case kEventClassMenu
:
532 result
= wxMacAppMenuEventHandler( handler
, event
, data
) ;
534 case kEventClassMouse
:
536 wxMacCarbonEvent
cEvent( event
) ;
539 Point screenMouseLocation
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ;
540 ::FindWindow(screenMouseLocation
, &window
);
541 // only send this event in case it had not already been sent to a tlw, as we get
542 // double events otherwise (in case event.skip) was called
543 if ( window
== NULL
)
544 result
= wxMacTopLevelMouseEventHandler( handler
, event
, NULL
) ;
547 case kEventClassAppleEvent
:
550 wxMacConvertEventToRecord( event
, &rec
) ;
551 result
= AEProcessAppleEvent( &rec
) ;
558 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerEventHandlerCallRef
) ;
563 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler
)
565 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
566 // we know it's there ;-)
567 WXIMPORT
char std::__throws_bad_alloc
;
572 pascal static void wxMacAssertOutputHandler(OSType componentSignature
, UInt32 options
,
573 const char *assertionString
, const char *exceptionLabelString
,
574 const char *errorString
, const char *fileName
, long lineNumber
, void *value
, ConstStr255Param outputMsg
)
576 // flow into assert handling
577 wxString fileNameStr
;
578 wxString assertionStr
;
579 wxString exceptionStr
;
582 fileNameStr
= wxString(fileName
, wxConvLocal
);
583 assertionStr
= wxString(assertionString
, wxConvLocal
);
584 exceptionStr
= wxString((exceptionLabelString
!=0) ? exceptionLabelString
: "", wxConvLocal
) ;
585 errorStr
= wxString((errorString
!=0) ? errorString
: "", wxConvLocal
) ;
587 fileNameStr
= fileName
;
588 assertionStr
= assertionString
;
589 exceptionStr
= (exceptionLabelString
!=0) ? exceptionLabelString
: "" ;
590 errorStr
= (errorString
!=0) ? errorString
: "" ;
595 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
596 assertionStr
.c_str() ,
597 exceptionStr
.c_str() ,
599 fileNameStr
.c_str(), lineNumber
,
603 wxOnAssert(fileNameStr
, lineNumber
, assertionStr
,
604 wxString::Format( wxT("%s %s value (%p)") ,exceptionStr
, errorStr
, value
) ) ;
612 /* m_macEventPosted run loop source callback: */
613 void macPostedEventCallback(void *unused
);
616 void macPostedEventCallback(void *unused
) {
617 wxTheApp
->ProcessPendingEvents(); }
620 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
625 InstallDebugAssertOutputHandler ( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler
) );
627 UMAInitToolbox( 4, sm_isEmbedded
) ;
628 SetEventMask( everyEvent
) ;
629 UMAShowWatchCursor() ;
632 # if __option(profile)
633 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
638 // now avoid exceptions thrown for new (bad_alloc)
639 // FIXME CS for some changes outside wxMac does not compile anymore
641 std::__throws_bad_alloc
= 0 ;
646 s_macCursorRgn
= ::NewRgn() ;
648 // Mac OS X passes a process serial number command line argument when
649 // the application is launched from the Finder. This argument must be
650 // removed from the command line arguments before being handled by the
651 // application (otherwise applications would need to handle it)
654 static const wxChar
*ARG_PSN
= _T("-psn_");
655 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
657 // remove this argument
659 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
663 if ( !wxAppBase::Initialize(argc
, argv
) )
667 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
670 #if TARGET_API_MAC_OSX
671 // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
672 wxString startupCwd
= wxGetCwd() ;
673 if ( startupCwd
== wxT("/") || startupCwd
.Right(15) == wxT("/Contents/MacOS") )
675 CFURLRef url
= CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
676 CFURLRef urlParent
= CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault
, url
) ;
678 CFStringRef path
= CFURLCopyFileSystemPath ( urlParent
, kCFURLPOSIXPathStyle
) ;
679 CFRelease( urlParent
) ;
680 wxString cwd
= wxMacCFStringHolder(path
).AsString(wxLocale::GetSystemEncoding());
681 wxSetWorkingDirectory( cwd
) ;
685 wxMacCreateNotifierTable() ;
688 /* connect posted events to common-mode run loop so that wxPostEvent events
689 are handled even while we're in the menu or on a scrollbar */
690 CFRunLoopSourceContext event_posted_context
= {0};
691 event_posted_context
.perform
= macPostedEventCallback
;
692 m_macEventPosted
= CFRunLoopSourceCreate(NULL
,0,&event_posted_context
);
693 CFRunLoopAddSource(CFRunLoopGetCurrent(), m_macEventPosted
, kCFRunLoopCommonModes
);
696 UMAShowArrowCursor() ;
701 bool wxApp::OnInitGui()
703 if( !wxAppBase::OnInitGui() )
706 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
710 InstallApplicationEventHandler(
711 GetwxMacAppEventHandlerUPP(),
712 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
717 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
718 NewAEEventHandlerUPP(AEHandleODoc
) ,
720 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
721 NewAEEventHandlerUPP(AEHandleOApp
) ,
723 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
724 NewAEEventHandlerUPP(AEHandlePDoc
) ,
726 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
727 NewAEEventHandlerUPP(AEHandleRApp
) ,
729 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
730 NewAEEventHandlerUPP(AEHandleQuit
) ,
737 void wxApp::CleanUp()
740 wxToolTip::RemoveToolTips() ;
744 if (m_macEventPosted
)
746 CFRelease(m_macEventPosted
);
748 m_macEventPosted
= NULL
;
751 // One last chance for pending objects to be cleaned up
752 wxTheApp
->DeletePendingObjects();
754 wxMacDestroyNotifierTable() ;
757 # if __option(profile)
758 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
763 UMACleanupToolbox() ;
764 if (s_macCursorRgn
) {
765 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
772 wxAppBase::CleanUp();
775 //----------------------------------------------------------------------
776 // misc initialization stuff
777 //----------------------------------------------------------------------
779 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
781 // for shared libraries we have to manually get the correct resource
782 // ref num upon initializing and releasing when terminating, therefore
783 // the __wxinitialize and __wxterminate must be used
786 void __sinit(void); /* (generated by linker) */
787 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
788 pascal void __terminate(void);
791 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
793 return __initialize( theInitBlock
) ;
796 pascal void __wxterminate(void)
801 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
803 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
805 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
806 OSStatus err
= noErr
;
809 switch( GetEventClass( event
) )
811 case kEventClassKeyboard
:
814 switch( GetEventKind(event
) )
816 case kEventRawKeyDown
:
817 rec
->what
= keyDown
;
819 case kEventRawKeyRepeat
:
820 rec
->what
= autoKey
;
822 case kEventRawKeyUp
:
825 case kEventRawKeyModifiersChanged
:
826 rec
->what
= nullEvent
;
835 unsigned char charCode
;
837 GetMouse( &rec
->where
) ;
839 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
840 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
841 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
842 rec
->modifiers
= modifiers
;
843 rec
->message
= (keyCode
<< 8 ) + charCode
;
847 case kEventClassTextInput
:
849 switch( GetEventKind( event
) )
851 case kEventTextInputUnicodeForKeyEvent
:
854 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
858 unsigned char charCode
;
860 GetMouse( &rec
->where
) ;
861 rec
->what
= keyDown
;
862 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
863 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
864 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
865 rec
->modifiers
= modifiers
;
866 rec
->message
= (keyCode
<< 8 ) + charCode
;
883 m_printMode
= wxPRINT_WINDOWS
;
885 m_macCurrentEvent
= NULL
;
886 m_macCurrentEventHandlerCallRef
= NULL
;
888 m_macEventPosted
= NULL
;
892 int wxApp::MainLoop()
896 RunApplicationEventLoop() ;
906 void wxApp::ExitMainLoop()
910 QuitApplicationEventLoop() ;
914 // Is a message/event pending?
915 bool wxApp::Pending()
917 // without the receive event (with pull param = false ) nothing is ever reported
919 ReceiveNextEvent (0, NULL
, kEventDurationNoWait
, false, &theEvent
);
920 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
923 // Dispatch a message.
924 bool wxApp::Dispatch()
931 void wxApp::OnIdle(wxIdleEvent
& event
)
933 wxAppBase::OnIdle(event
);
935 // If they are pending events, we must process them: pending events are
936 // either events to the threads other than main or events posted with
937 // wxPostEvent() functions
938 wxMacProcessNotifierAndPendingEvents();
940 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
941 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
944 void wxApp::WakeUpIdle()
947 if (m_macEventPosted
)
949 CFRunLoopSourceSignal(m_macEventPosted
);
961 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
964 GetTopWindow()->Close(TRUE
);
967 // Default behaviour: close the application with prompts. The
968 // user can veto the close, and therefore the end session.
969 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
973 if (!GetTopWindow()->Close(!event
.CanVeto()))
978 extern "C" void wxCYield() ;
984 // Yield to other processes
986 bool wxApp::Yield(bool onlyIfNeeded
)
992 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1000 // by definition yield should handle all non-processed events
1004 OSStatus status
= noErr
;
1007 s_inReceiveEvent
= true ;
1008 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1009 s_inReceiveEvent
= false ;
1011 if ( status
== eventLoopTimedOutErr
)
1013 // make sure next time the event loop will trigger idle events
1014 sleepTime
= kEventDurationNoWait
;
1016 else if ( status
== eventLoopQuitErr
)
1018 // according to QA1061 this may also occur when a WakeUp Process
1023 MacHandleOneEvent( theEvent
) ;
1024 ReleaseEvent(theEvent
);
1026 } while( status
== noErr
) ;
1028 wxMacProcessNotifierAndPendingEvents() ;
1034 void wxApp::MacDoOneEvent()
1038 s_inReceiveEvent
= true ;
1039 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1040 s_inReceiveEvent
= false ;
1041 if ( status
== eventLoopTimedOutErr
)
1043 if ( wxTheApp
->ProcessIdle() )
1044 sleepTime
= kEventDurationNoWait
;
1046 sleepTime
= kEventDurationSecond
;
1048 else if ( status
== eventLoopQuitErr
)
1050 // according to QA1061 this may also occur when a WakeUp Process
1055 MacHandleOneEvent( theEvent
) ;
1056 ReleaseEvent(theEvent
);
1057 sleepTime
= kEventDurationNoWait
;
1061 DeletePendingObjects() ;
1062 wxMacProcessNotifierAndPendingEvents() ;
1065 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1067 // Override to process unhandled events as you please
1070 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1072 EventTargetRef theTarget
;
1073 theTarget
= GetEventDispatcherTarget();
1074 m_macCurrentEvent
= evr
;
1075 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1076 if(status
== eventNotHandledErr
)
1078 MacHandleUnhandledEvent(evr
);
1080 wxMacProcessNotifierAndPendingEvents() ;
1082 wxMutexGuiLeaveOrEnter();
1083 #endif // wxUSE_THREADS
1086 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1087 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1092 case kHomeCharCode
:
1095 case kEnterCharCode
:
1096 retval
= WXK_RETURN
;
1101 case kHelpCharCode
:
1104 case kBackspaceCharCode
:
1110 case kPageUpCharCode
:
1111 retval
= WXK_PAGEUP
;
1113 case kPageDownCharCode
:
1114 retval
= WXK_PAGEDOWN
;
1116 case kReturnCharCode
:
1117 retval
= WXK_RETURN
;
1119 case kFunctionKeyCharCode
:
1171 case kEscapeCharCode
:
1172 retval
= WXK_ESCAPE
;
1174 case kLeftArrowCharCode
:
1177 case kRightArrowCharCode
:
1178 retval
= WXK_RIGHT
;
1180 case kUpArrowCharCode
:
1183 case kDownArrowCharCode
:
1186 case kDeleteCharCode
:
1187 retval
= WXK_DELETE
;
1195 int wxMacKeyCodeToModifier(wxKeyCode key
)
1221 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1223 wxASSERT_MSG(key
!= WXK_LBUTTON
&& key
!= WXK_RBUTTON
&& key
!=
1224 WXK_MBUTTON
, wxT("can't use wxGetKeyState() for mouse buttons"));
1226 //if OS X > 10.2 (i.e. 10.2.x)
1227 //a known apple bug prevents the system from determining led
1228 //states with GetKeys... can only determine caps lock led
1229 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key
));
1231 // KeyMapByteArray keymap;
1232 // GetKeys((BigEndianLong*)keymap);
1233 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1238 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1245 keychar
= short(keymessage
& charCodeMask
);
1246 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1248 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1250 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1251 // and look at the character after
1253 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1254 keychar
= short(keyInfo
& charCodeMask
);
1255 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1257 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1258 long realkeyval
= keyval
;
1259 if ( keyval
== keychar
)
1261 // 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)
1262 realkeyval
= short(keymessage
& charCodeMask
) ;
1263 keyval
= wxToupper( keyval
) ;
1266 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1267 bool handled
= false ;
1268 event
.m_shiftDown
= modifiers
& shiftKey
;
1269 event
.m_controlDown
= modifiers
& controlKey
;
1270 event
.m_altDown
= modifiers
& optionKey
;
1271 event
.m_metaDown
= modifiers
& cmdKey
;
1272 event
.m_keyCode
= keyval
;
1274 event
.m_uniChar
= uniChar
;
1279 event
.SetTimestamp(when
);
1280 event
.SetEventObject(focus
);
1281 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1282 if ( handled
&& event
.GetSkipped() )
1289 wxWindow
*ancestor
= focus
;
1292 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1295 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1296 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1299 if (ancestor
->IsTopLevel())
1301 ancestor
= ancestor
->GetParent();
1304 #endif // wxUSE_ACCEL
1308 wxTopLevelWindowMac
*tlw
= focus
->MacGetTopLevelWindow() ;
1312 event
.Skip( FALSE
) ;
1313 event
.SetEventType( wxEVT_CHAR_HOOK
);
1315 event
.m_keyCode
= realkeyval
;
1317 handled
= tlw
->GetEventHandler()->ProcessEvent( event
);
1318 if ( handled
&& event
.GetSkipped() )
1325 event
.Skip( FALSE
) ;
1326 event
.SetEventType( wxEVT_CHAR
) ;
1328 event
.m_keyCode
= realkeyval
;
1330 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1331 if ( handled
&& event
.GetSkipped() )
1334 if ( !handled
&& (keyval
== WXK_TAB
) )
1336 wxWindow
* iter
= focus
->GetParent() ;
1337 while( iter
&& !handled
)
1339 if ( iter
->HasFlag( wxTAB_TRAVERSAL
) )
1341 wxNavigationKeyEvent new_event
;
1342 new_event
.SetEventObject( focus
);
1343 new_event
.SetDirection( !event
.ShiftDown() );
1344 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1345 new_event
.SetWindowChange( event
.ControlDown() );
1346 new_event
.SetCurrentFocus( focus
);
1347 handled
= focus
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
1348 if ( handled
&& new_event
.GetSkipped() )
1351 iter
= iter
->GetParent() ;
1354 // backdoor handler for default return and command escape
1355 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1357 // if window is not having a focus still testing for default enter or cancel
1358 // TODO add the UMA version for ActiveNonFloatingWindow
1359 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1362 if ( keyval
== WXK_RETURN
)
1364 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1366 if ( def
&& def
->IsEnabled() )
1368 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1369 event
.SetEventObject(def
);
1370 def
->Command(event
);
1374 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1375 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1377 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1378 new_event
.SetEventObject( focus
);
1379 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1386 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
, wxChar uniChar
)
1393 keychar
= short(keymessage
& charCodeMask
);
1394 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1395 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1397 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1398 // and look at the character after
1400 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1401 keychar
= short(keyInfo
& charCodeMask
);
1402 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1404 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1406 if ( keyval
== keychar
)
1408 keyval
= wxToupper( keyval
) ;
1410 bool handled
= false ;
1412 wxKeyEvent
event(wxEVT_KEY_UP
);
1413 event
.m_shiftDown
= modifiers
& shiftKey
;
1414 event
.m_controlDown
= modifiers
& controlKey
;
1415 event
.m_altDown
= modifiers
& optionKey
;
1416 event
.m_metaDown
= modifiers
& cmdKey
;
1417 event
.m_keyCode
= keyval
;
1419 event
.m_uniChar
= uniChar
;
1424 event
.SetTimestamp(when
);
1425 event
.SetEventObject(focus
);
1426 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;