1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "app.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>
62 // include hid keyboard
63 # include "wx/mac/carbon/private/hid.h"
67 # include <ToolUtils.h>
68 # include <DiskInit.h>
72 extern wxList wxPendingDelete
;
74 // set wxMAC_USE_RAEL to 1 if RunApplicationEventLoop should be used
75 // if 0 the lower level CarbonEventLoop will be used
76 // on the long run RAEL should replace the low level event loop
77 // we will have to clean up event handling to make sure we don't
78 // miss handling of things like pending events etc
79 // perhaps we will also have to pipe events through an ueber-event-handler
80 // to make sure we have one place to do all these house-keeping functions
82 #define wxMAC_USE_RAEL 0
85 extern size_t g_numberOfThreads
;
86 #endif // wxUSE_THREADS
88 // statics for implementation
90 static bool s_inYield
= FALSE
;
92 static bool s_inReceiveEvent
= FALSE
;
93 static EventTime sleepTime
= kEventDurationNoWait
;
95 #if !USE_SHARED_LIBRARY
96 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
97 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
98 EVT_IDLE(wxApp::OnIdle
)
99 EVT_END_SESSION(wxApp::OnEndSession
)
100 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
105 const short kMacMinHeap
= (29 * 1024) ;
106 // platform specific static variables
108 const short kwxMacMenuBarResource
= 1 ;
109 const short kwxMacAppleMenuId
= 1 ;
111 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
112 wxWindow
* wxApp::s_captureWindow
= NULL
;
113 int wxApp::s_lastMouseDown
= 0 ;
114 long wxApp::sm_lastMessageTime
= 0;
115 long wxApp::s_lastModifiers
= 0 ;
118 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
119 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
120 long wxApp::s_macPreferencesMenuItemId
= wxID_PREFERENCES
;
121 long wxApp::s_macExitMenuItemId
= wxID_EXIT
;
122 wxString
wxApp::s_macHelpMenuTitleName
= wxT("&Help") ;
125 wxHIDKeyboard
* wxApp::s_macHIDKeyboard
= NULL
;
128 // Normally we're not a plugin
129 bool wxApp::sm_isEmbedded
= false;
130 //----------------------------------------------------------------------
131 // Core Apple Event Support
132 //----------------------------------------------------------------------
134 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
135 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
136 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
137 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
138 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
140 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
142 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
145 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
147 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
150 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
152 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
155 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
157 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
160 pascal OSErr
AEHandleRApp( const AppleEvent
*event
, AppleEvent
*reply
, long WXUNUSED(refcon
) )
162 return wxTheApp
->MacHandleAERApp( (AppleEvent
*) event
, reply
) ;
165 // AEODoc Calls MacOpenFile on each of the files passed
167 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
171 DescType returnedType
;
176 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
180 err
= AECountItems(&docList
, &itemsInList
);
184 ProcessSerialNumber PSN
;
185 PSN
.highLongOfPSN
= 0 ;
186 PSN
.lowLongOfPSN
= kCurrentProcess
;
187 SetFrontProcess( &PSN
) ;
189 for (i
= 1; i
<= itemsInList
; i
++)
194 AEGetNthPtr(&docList
, i
, typeFSRef
, &keywd
, &returnedType
,
195 (Ptr
) & theRef
, sizeof(theRef
), &actualSize
);
196 fName
= wxMacFSRefToPath( &theRef
) ;
203 // AEPDoc Calls MacPrintFile on each of the files passed
205 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
209 DescType returnedType
;
214 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
218 err
= AECountItems(&docList
, &itemsInList
);
222 ProcessSerialNumber PSN
;
223 PSN
.highLongOfPSN
= 0 ;
224 PSN
.lowLongOfPSN
= kCurrentProcess
;
225 SetFrontProcess( &PSN
) ;
227 for (i
= 1; i
<= itemsInList
; i
++) {
231 AEGetNthPtr(&docList
, i
, typeFSRef
, &keywd
, &returnedType
,
232 (Ptr
) & theRef
, sizeof(theRef
), &actualSize
);
233 fName
= wxMacFSRefToPath( &theRef
) ;
240 // AEOApp calls MacNewFile
242 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
248 // AEQuit attempts to quit the application
250 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
252 wxWindow
* win
= GetTopWindow() ;
255 wxCommandEvent
exitEvent(wxEVT_COMMAND_MENU_SELECTED
, s_macExitMenuItemId
);
256 if (!win
->ProcessEvent(exitEvent
))
266 // AEROApp calls MacReopenApp
268 short wxApp::MacHandleAERApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
275 //----------------------------------------------------------------------
276 // Support Routines linking the Mac...File Calls to the Document Manager
277 //----------------------------------------------------------------------
279 void wxApp::MacOpenFile(const wxString
& fileName
)
281 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
283 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
286 void wxApp::MacPrintFile(const wxString
& fileName
)
288 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
291 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
294 wxView
* view
= doc
->GetFirstView() ;
297 wxPrintout
*printout
= view
->OnCreatePrintout();
301 printer
.Print(view
->GetFrame(), printout
, TRUE
);
307 doc
->DeleteAllViews();
308 dm
->RemoveDocument(doc
) ;
314 void wxApp::MacNewFile()
318 void wxApp::MacReopenApp()
321 // if there is no open window -> create a new one
322 // if all windows are hidden -> show the first
323 // if some windows are not hidden -> do nothing
325 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
332 wxTopLevelWindow
* firstIconized
= NULL
;
335 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->GetData();
336 if ( win
->IsIconized() == false )
338 firstIconized
= NULL
;
343 if ( firstIconized
== NULL
)
344 firstIconized
= win
;
346 node
= node
->GetNext();
349 firstIconized
->Iconize( false ) ;
353 //----------------------------------------------------------------------
354 // Carbon Event Handler
355 //----------------------------------------------------------------------
357 static const EventTypeSpec eventList
[] =
359 { kEventClassCommand
, kEventProcessCommand
} ,
360 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
362 { kEventClassMenu
, kEventMenuOpening
},
363 { kEventClassMenu
, kEventMenuClosed
},
364 { kEventClassMenu
, kEventMenuTargetItem
},
366 { kEventClassApplication
, kEventAppActivated
} ,
367 { kEventClassApplication
, kEventAppDeactivated
} ,
368 // handling the quit event is not recommended by apple
369 // rather using the quit apple event - which we do
371 { kEventClassAppleEvent
, kEventAppleEvent
} ,
373 { kEventClassMouse
, kEventMouseDown
} ,
374 { kEventClassMouse
, kEventMouseMoved
} ,
375 { kEventClassMouse
, kEventMouseUp
} ,
376 { kEventClassMouse
, kEventMouseDragged
} ,
380 static pascal OSStatus
381 wxMacAppMenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
383 wxMacCarbonEvent
cEvent( event
) ;
384 MenuRef menuRef
= cEvent
.GetParameter
<MenuRef
>(kEventParamDirectObject
) ;
385 wxMenu
* menu
= wxFindMenuFromMacMenu( menuRef
) ;
391 switch (GetEventKind(event
))
393 case kEventMenuOpening
:
394 type
= wxEVT_MENU_OPEN
;
396 case kEventMenuClosed
:
397 type
= wxEVT_MENU_CLOSE
;
399 case kEventMenuTargetItem
:
400 cmd
= cEvent
.GetParameter
<MenuCommand
>(kEventParamMenuCommand
,typeMenuCommand
) ;
402 type
= wxEVT_MENU_HIGHLIGHT
;
405 wxFAIL_MSG(wxT("Unexpected menu event kind"));
411 wxMenuEvent
wxevent(type
, cmd
, menu
);
412 wxevent
.SetEventObject(menu
);
414 wxEvtHandler
* handler
= menu
->GetEventHandler();
415 if (handler
&& handler
->ProcessEvent(wxevent
))
421 wxWindow
*win
= menu
->GetInvokingWindow();
423 win
->GetEventHandler()->ProcessEvent(wxevent
);
428 return eventNotHandledErr
;
431 static pascal OSStatus
wxMacAppCommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
433 OSStatus result
= eventNotHandledErr
;
437 wxMacCarbonEvent
cEvent( event
) ;
438 cEvent
.GetParameter
<HICommand
>(kEventParamDirectObject
,typeHICommand
,&command
) ;
440 wxMenuItem
* item
= NULL
;
441 MenuCommand id
= command
.commandID
;
442 // for items we don't really control
443 if ( id
== kHICommandPreferences
)
445 id
= wxApp::s_macPreferencesMenuItemId
;
447 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
450 wxMenu
* menu
= NULL
;
451 item
= mbar
->FindItem( id
, &menu
) ;
454 else if ( id
!= 0 && command
.menu
.menuRef
!= 0 && command
.menu
.menuItemIndex
!= 0 )
456 GetMenuItemRefCon( command
.menu
.menuRef
, command
.menu
.menuItemIndex
, (UInt32
*) &item
) ;
461 switch( cEvent
.GetKind() )
463 case kEventProcessCommand
:
465 if (item
->IsCheckable())
467 item
->Check( !item
->IsChecked() ) ;
470 item
->GetMenu()->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
474 case kEventCommandUpdateStatus
:
475 // eventually trigger an updateui round
485 static pascal OSStatus
wxMacAppApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
487 OSStatus result
= eventNotHandledErr
;
488 switch ( GetEventKind( event
) )
490 case kEventAppActivated
:
493 wxTheApp
->SetActive( true , NULL
) ;
497 case kEventAppDeactivated
:
500 wxTheApp
->SetActive( false , NULL
) ;
510 pascal OSStatus
wxMacAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
512 EventRef formerEvent
= (EventRef
) wxTheApp
->MacGetCurrentEvent() ;
513 EventHandlerCallRef formerEventHandlerCallRef
= (EventHandlerCallRef
) wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
514 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
516 OSStatus result
= eventNotHandledErr
;
517 switch( GetEventClass( event
) )
519 case kEventClassCommand
:
520 result
= wxMacAppCommandEventHandler( handler
, event
, data
) ;
522 case kEventClassApplication
:
523 result
= wxMacAppApplicationEventHandler( handler
, event
, data
) ;
525 case kEventClassMenu
:
526 result
= wxMacAppMenuEventHandler( handler
, event
, data
) ;
528 case kEventClassMouse
:
530 wxMacCarbonEvent
cEvent( event
) ;
533 Point screenMouseLocation
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ;
534 ::FindWindow(screenMouseLocation
, &window
);
535 // only send this event in case it had not already been sent to a tlw, as we get
536 // double events otherwise (in case event.skip) was called
537 if ( window
== NULL
)
538 result
= wxMacTopLevelMouseEventHandler( handler
, event
, NULL
) ;
541 case kEventClassAppleEvent
:
544 wxMacConvertEventToRecord( event
, &rec
) ;
545 result
= AEProcessAppleEvent( &rec
) ;
552 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerEventHandlerCallRef
) ;
557 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler
)
559 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
560 // we know it's there ;-)
561 WXIMPORT
char std::__throws_bad_alloc
;
566 pascal static void wxMacAssertOutputHandler(OSType componentSignature
, UInt32 options
,
567 const char *assertionString
, const char *exceptionLabelString
,
568 const char *errorString
, const char *fileName
, long lineNumber
, void *value
, ConstStr255Param outputMsg
)
570 // flow into assert handling
571 wxString fileNameStr
;
572 wxString assertionStr
;
573 wxString exceptionStr
;
576 fileNameStr
= wxString(fileName
, wxConvLocal
);
577 assertionStr
= wxString(assertionString
, wxConvLocal
);
578 exceptionStr
= wxString((exceptionLabelString
!=0) ? exceptionLabelString
: "", wxConvLocal
) ;
579 errorStr
= wxString((errorString
!=0) ? errorString
: "", wxConvLocal
) ;
581 fileNameStr
= fileName
;
582 assertionStr
= assertionString
;
583 exceptionStr
= (exceptionLabelString
!=0) ? exceptionLabelString
: "" ;
584 errorStr
= (errorString
!=0) ? errorString
: "" ;
589 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
590 assertionStr
.c_str() ,
591 exceptionStr
.c_str() ,
593 fileNameStr
.c_str(), lineNumber
,
597 wxOnAssert(fileNameStr
, lineNumber
, assertionStr
,
598 wxString::Format( wxT("%s %s value (%p)") ,exceptionStr
, errorStr
, value
) ) ;
604 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
609 InstallDebugAssertOutputHandler ( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler
) );
611 UMAInitToolbox( 4, sm_isEmbedded
) ;
612 SetEventMask( everyEvent
) ;
613 UMAShowWatchCursor() ;
616 # if __option(profile)
617 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
622 // now avoid exceptions thrown for new (bad_alloc)
623 // FIXME CS for some changes outside wxMac does not compile anymore
625 std::__throws_bad_alloc
= 0 ;
630 s_macCursorRgn
= ::NewRgn() ;
632 // Mac OS X passes a process serial number command line argument when
633 // the application is launched from the Finder. This argument must be
634 // removed from the command line arguments before being handled by the
635 // application (otherwise applications would need to handle it)
638 static const wxChar
*ARG_PSN
= _T("-psn_");
639 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
641 // remove this argument
643 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
647 if ( !wxAppBase::Initialize(argc
, argv
) )
651 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
654 #if TARGET_API_MAC_OSX
655 // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
656 wxString startupCwd
= wxGetCwd() ;
657 if ( startupCwd
== wxT("/") || startupCwd
.Right(15) == wxT("/Contents/MacOS") )
659 CFURLRef url
= CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
660 CFURLRef urlParent
= CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault
, url
) ;
662 CFStringRef path
= CFURLCopyFileSystemPath ( urlParent
, kCFURLPOSIXPathStyle
) ;
663 CFRelease( urlParent
) ;
664 wxString cwd
= wxMacCFStringHolder(path
).AsString(wxLocale::GetSystemEncoding());
665 wxSetWorkingDirectory( cwd
) ;
669 wxMacCreateNotifierTable() ;
671 UMAShowArrowCursor() ;
676 bool wxApp::OnInitGui()
678 if( !wxAppBase::OnInitGui() )
681 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
685 InstallApplicationEventHandler(
686 GetwxMacAppEventHandlerUPP(),
687 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
692 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
693 NewAEEventHandlerUPP(AEHandleODoc
) ,
695 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
696 NewAEEventHandlerUPP(AEHandleOApp
) ,
698 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
699 NewAEEventHandlerUPP(AEHandlePDoc
) ,
701 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
702 NewAEEventHandlerUPP(AEHandleRApp
) ,
704 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
705 NewAEEventHandlerUPP(AEHandleQuit
) ,
712 void wxApp::CleanUp()
714 wxToolTip::RemoveToolTips() ;
716 // One last chance for pending objects to be cleaned up
717 wxTheApp
->DeletePendingObjects();
719 wxMacDestroyNotifierTable() ;
722 # if __option(profile)
723 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
729 // clean up HID Keyboard
730 if (s_macHIDKeyboard
)
731 delete s_macHIDKeyboard
;
734 UMACleanupToolbox() ;
735 if (s_macCursorRgn
) {
736 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
743 wxAppBase::CleanUp();
746 //----------------------------------------------------------------------
747 // misc initialization stuff
748 //----------------------------------------------------------------------
750 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
752 // for shared libraries we have to manually get the correct resource
753 // ref num upon initializing and releasing when terminating, therefore
754 // the __wxinitialize and __wxterminate must be used
757 void __sinit(void); /* (generated by linker) */
758 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
759 pascal void __terminate(void);
762 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
764 return __initialize( theInitBlock
) ;
767 pascal void __wxterminate(void)
772 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
774 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
776 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
777 OSStatus err
= noErr
;
780 switch( GetEventClass( event
) )
782 case kEventClassKeyboard
:
785 switch( GetEventKind(event
) )
787 case kEventRawKeyDown
:
788 rec
->what
= keyDown
;
790 case kEventRawKeyRepeat
:
791 rec
->what
= autoKey
;
793 case kEventRawKeyUp
:
796 case kEventRawKeyModifiersChanged
:
797 rec
->what
= nullEvent
;
806 unsigned char charCode
;
808 GetMouse( &rec
->where
) ;
810 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
811 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
812 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
813 rec
->modifiers
= modifiers
;
814 rec
->message
= (keyCode
<< 8 ) + charCode
;
818 case kEventClassTextInput
:
820 switch( GetEventKind( event
) )
822 case kEventTextInputUnicodeForKeyEvent
:
825 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
829 unsigned char charCode
;
831 GetMouse( &rec
->where
) ;
832 rec
->what
= keyDown
;
833 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
834 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
835 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
836 rec
->modifiers
= modifiers
;
837 rec
->message
= (keyCode
<< 8 ) + charCode
;
854 m_printMode
= wxPRINT_WINDOWS
;
856 m_macCurrentEvent
= NULL
;
857 m_macCurrentEventHandlerCallRef
= NULL
;
860 int wxApp::MainLoop()
864 RunApplicationEventLoop() ;
874 void wxApp::ExitMainLoop()
878 QuitApplicationEventLoop() ;
882 // Is a message/event pending?
883 bool wxApp::Pending()
885 // without the receive event (with pull param = false ) nothing is ever reported
887 ReceiveNextEvent (0, NULL
, kEventDurationNoWait
, false, &theEvent
);
888 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
891 // Dispatch a message.
892 bool wxApp::Dispatch()
899 void wxApp::OnIdle(wxIdleEvent
& event
)
901 wxAppBase::OnIdle(event
);
903 // If they are pending events, we must process them: pending events are
904 // either events to the threads other than main or events posted with
905 // wxPostEvent() functions
906 wxMacProcessNotifierAndPendingEvents();
908 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
909 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
912 void wxApp::WakeUpIdle()
923 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
926 GetTopWindow()->Close(TRUE
);
929 // Default behaviour: close the application with prompts. The
930 // user can veto the close, and therefore the end session.
931 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
935 if (!GetTopWindow()->Close(!event
.CanVeto()))
940 extern "C" void wxCYield() ;
946 // Yield to other processes
948 bool wxApp::Yield(bool onlyIfNeeded
)
954 wxFAIL_MSG( wxT("wxYield called recursively" ) );
962 // by definition yield should handle all non-processed events
966 OSStatus status
= noErr
;
969 s_inReceiveEvent
= true ;
970 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
971 s_inReceiveEvent
= false ;
973 if ( status
== eventLoopTimedOutErr
)
975 // make sure next time the event loop will trigger idle events
976 sleepTime
= kEventDurationNoWait
;
978 else if ( status
== eventLoopQuitErr
)
980 // according to QA1061 this may also occur when a WakeUp Process
985 MacHandleOneEvent( theEvent
) ;
986 ReleaseEvent(theEvent
);
988 } while( status
== noErr
) ;
990 wxMacProcessNotifierAndPendingEvents() ;
996 void wxApp::MacDoOneEvent()
1000 s_inReceiveEvent
= true ;
1001 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1002 s_inReceiveEvent
= false ;
1003 if ( status
== eventLoopTimedOutErr
)
1005 if ( wxTheApp
->ProcessIdle() )
1006 sleepTime
= kEventDurationNoWait
;
1008 sleepTime
= kEventDurationSecond
;
1010 else if ( status
== eventLoopQuitErr
)
1012 // according to QA1061 this may also occur when a WakeUp Process
1017 MacHandleOneEvent( theEvent
) ;
1018 ReleaseEvent(theEvent
);
1019 sleepTime
= kEventDurationNoWait
;
1023 DeletePendingObjects() ;
1024 wxMacProcessNotifierAndPendingEvents() ;
1027 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1029 // Override to process unhandled events as you please
1032 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1034 EventTargetRef theTarget
;
1035 theTarget
= GetEventDispatcherTarget();
1036 m_macCurrentEvent
= evr
;
1037 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1038 if(status
== eventNotHandledErr
)
1040 MacHandleUnhandledEvent(evr
);
1042 wxMacProcessNotifierAndPendingEvents() ;
1044 wxMutexGuiLeaveOrEnter();
1045 #endif // wxUSE_THREADS
1048 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1049 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1054 case kHomeCharCode
:
1057 case kEnterCharCode
:
1058 retval
= WXK_RETURN
;
1063 case kHelpCharCode
:
1066 case kBackspaceCharCode
:
1072 case kPageUpCharCode
:
1073 retval
= WXK_PAGEUP
;
1075 case kPageDownCharCode
:
1076 retval
= WXK_PAGEDOWN
;
1078 case kReturnCharCode
:
1079 retval
= WXK_RETURN
;
1081 case kFunctionKeyCharCode
:
1133 case kEscapeCharCode
:
1134 retval
= WXK_ESCAPE
;
1136 case kLeftArrowCharCode
:
1139 case kRightArrowCharCode
:
1140 retval
= WXK_RIGHT
;
1142 case kUpArrowCharCode
:
1145 case kDownArrowCharCode
:
1148 case kDeleteCharCode
:
1149 retval
= WXK_DELETE
;
1157 int wxMacKeyCodeToModifier(wxKeyCode key
)
1182 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1185 // Startup HID keyboard for getting key codes on DARWIN
1186 if (!wxApp::s_macHIDKeyboard
)
1188 wxApp::s_macHIDKeyboard
= new wxHIDKeyboard();
1189 wxApp::s_macHIDKeyboard
->Create();
1192 return wxApp::s_macHIDKeyboard
->IsActive(key
);
1194 //if OS X > 10.2 (i.e. 10.2.x)
1195 //a known apple bug prevents the system from determining led
1196 //states with GetKeys... can only determine caps lock led
1197 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key
));
1199 // KeyMapByteArray keymap;
1200 // GetKeys((BigEndianLong*)keymap);
1201 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1206 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1213 keychar
= short(keymessage
& charCodeMask
);
1214 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1216 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1218 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1219 // and look at the character after
1221 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1222 keychar
= short(keyInfo
& charCodeMask
);
1223 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1225 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1226 long realkeyval
= keyval
;
1227 if ( keyval
== keychar
)
1229 // 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)
1230 realkeyval
= short(keymessage
& charCodeMask
) ;
1231 keyval
= wxToupper( keyval
) ;
1234 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1235 bool handled
= false ;
1236 event
.m_shiftDown
= modifiers
& shiftKey
;
1237 event
.m_controlDown
= modifiers
& controlKey
;
1238 event
.m_altDown
= modifiers
& optionKey
;
1239 event
.m_metaDown
= modifiers
& cmdKey
;
1240 event
.m_keyCode
= keyval
;
1244 event
.SetTimestamp(when
);
1245 event
.SetEventObject(focus
);
1246 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1247 if ( handled
&& event
.GetSkipped() )
1254 wxWindow
*ancestor
= focus
;
1257 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1260 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1261 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1264 if (ancestor
->IsTopLevel())
1266 ancestor
= ancestor
->GetParent();
1269 #endif // wxUSE_ACCEL
1273 event
.Skip( FALSE
) ;
1274 event
.SetEventType( wxEVT_CHAR
) ;
1276 event
.m_keyCode
= realkeyval
;
1278 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1279 if ( handled
&& event
.GetSkipped() )
1282 if ( !handled
&& (keyval
== WXK_TAB
) )
1284 wxWindow
* iter
= focus
->GetParent() ;
1285 while( iter
&& !handled
)
1287 if ( iter
->HasFlag( wxTAB_TRAVERSAL
) )
1289 wxNavigationKeyEvent new_event
;
1290 new_event
.SetEventObject( focus
);
1291 new_event
.SetDirection( !event
.ShiftDown() );
1292 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1293 new_event
.SetWindowChange( event
.ControlDown() );
1294 new_event
.SetCurrentFocus( focus
);
1295 handled
= focus
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
1296 if ( handled
&& new_event
.GetSkipped() )
1299 iter
= iter
->GetParent() ;
1302 // backdoor handler for default return and command escape
1303 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1305 // if window is not having a focus still testing for default enter or cancel
1306 // TODO add the UMA version for ActiveNonFloatingWindow
1307 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1310 if ( keyval
== WXK_RETURN
)
1312 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1314 if ( def
&& def
->IsEnabled() )
1316 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1317 event
.SetEventObject(def
);
1318 def
->Command(event
);
1322 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1323 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1325 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1326 new_event
.SetEventObject( focus
);
1327 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1334 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1341 keychar
= short(keymessage
& charCodeMask
);
1342 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1343 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1345 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1346 // and look at the character after
1348 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1349 keychar
= short(keyInfo
& charCodeMask
);
1350 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1352 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1354 if ( keyval
== keychar
)
1356 keyval
= wxToupper( keyval
) ;
1358 bool handled
= false ;
1360 wxKeyEvent
event(wxEVT_KEY_UP
);
1361 event
.m_shiftDown
= modifiers
& shiftKey
;
1362 event
.m_controlDown
= modifiers
& controlKey
;
1363 event
.m_altDown
= modifiers
& optionKey
;
1364 event
.m_metaDown
= modifiers
& cmdKey
;
1365 event
.m_keyCode
= keyval
;
1369 event
.SetTimestamp(when
);
1370 event
.SetEventObject(focus
);
1371 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;