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>
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
))
269 //----------------------------------------------------------------------
270 // Support Routines linking the Mac...File Calls to the Document Manager
271 //----------------------------------------------------------------------
273 void wxApp::MacOpenFile(const wxString
& fileName
)
275 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
277 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
280 void wxApp::MacPrintFile(const wxString
& fileName
)
282 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
285 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
288 wxView
* view
= doc
->GetFirstView() ;
291 wxPrintout
*printout
= view
->OnCreatePrintout();
295 printer
.Print(view
->GetFrame(), printout
, TRUE
);
301 doc
->DeleteAllViews();
302 dm
->RemoveDocument(doc
) ;
308 void wxApp::MacNewFile()
312 void wxApp::MacReopenApp()
315 // if there is no open window -> create a new one
316 // if all windows are hidden -> show the first
317 // if some windows are not hidden -> do nothing
319 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
326 wxTopLevelWindow
* firstIconized
= NULL
;
329 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->GetData();
330 if ( win
->IsIconized() == false )
332 firstIconized
= NULL
;
337 if ( firstIconized
== NULL
)
338 firstIconized
= win
;
340 node
= node
->GetNext();
343 firstIconized
->Iconize( false ) ;
347 //----------------------------------------------------------------------
348 // Carbon Event Handler
349 //----------------------------------------------------------------------
351 static const EventTypeSpec eventList
[] =
353 { kEventClassCommand
, kEventProcessCommand
} ,
354 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
356 { kEventClassMenu
, kEventMenuOpening
},
357 { kEventClassMenu
, kEventMenuClosed
},
358 { kEventClassMenu
, kEventMenuTargetItem
},
360 { kEventClassApplication
, kEventAppActivated
} ,
361 { kEventClassApplication
, kEventAppDeactivated
} ,
362 // handling the quit event is not recommended by apple
363 // rather using the quit apple event - which we do
365 { kEventClassAppleEvent
, kEventAppleEvent
} ,
367 { kEventClassMouse
, kEventMouseDown
} ,
368 { kEventClassMouse
, kEventMouseMoved
} ,
369 { kEventClassMouse
, kEventMouseUp
} ,
370 { kEventClassMouse
, kEventMouseDragged
} ,
374 static pascal OSStatus
375 wxMacAppMenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
377 wxMacCarbonEvent
cEvent( event
) ;
378 MenuRef menuRef
= cEvent
.GetParameter
<MenuRef
>(kEventParamDirectObject
) ;
379 wxMenu
* menu
= wxFindMenuFromMacMenu( menuRef
) ;
385 switch (GetEventKind(event
))
387 case kEventMenuOpening
:
388 type
= wxEVT_MENU_OPEN
;
390 case kEventMenuClosed
:
391 type
= wxEVT_MENU_CLOSE
;
393 case kEventMenuTargetItem
:
394 cmd
= cEvent
.GetParameter
<MenuCommand
>(kEventParamMenuCommand
,typeMenuCommand
) ;
396 type
= wxEVT_MENU_HIGHLIGHT
;
399 wxFAIL_MSG(wxT("Unexpected menu event kind"));
405 wxMenuEvent
wxevent(type
, cmd
);
406 wxevent
.SetEventObject(menu
);
408 wxEvtHandler
* handler
= menu
->GetEventHandler();
409 if (handler
&& handler
->ProcessEvent(wxevent
))
415 wxWindow
*win
= menu
->GetInvokingWindow();
417 win
->GetEventHandler()->ProcessEvent(wxevent
);
422 return eventNotHandledErr
;
425 static pascal OSStatus
wxMacAppCommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
427 OSStatus result
= eventNotHandledErr
;
431 wxMacCarbonEvent
cEvent( event
) ;
432 cEvent
.GetParameter
<HICommand
>(kEventParamDirectObject
,typeHICommand
,&command
) ;
434 wxMenuItem
* item
= NULL
;
435 MenuCommand id
= command
.commandID
;
436 // for items we don't really control
437 if ( id
== kHICommandPreferences
)
439 id
= wxApp::s_macPreferencesMenuItemId
;
441 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
444 wxMenu
* menu
= NULL
;
445 item
= mbar
->FindItem( id
, &menu
) ;
448 else if ( id
!= 0 && command
.menu
.menuRef
!= 0 && command
.menu
.menuItemIndex
!= 0 )
450 GetMenuItemRefCon( command
.menu
.menuRef
, command
.menu
.menuItemIndex
, (UInt32
*) &item
) ;
455 switch( cEvent
.GetKind() )
457 case kEventProcessCommand
:
459 if (item
->IsCheckable())
461 item
->Check( !item
->IsChecked() ) ;
464 item
->GetMenu()->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
468 case kEventCommandUpdateStatus
:
469 // eventually trigger an updateui round
479 static pascal OSStatus
wxMacAppApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
481 OSStatus result
= eventNotHandledErr
;
482 switch ( GetEventKind( event
) )
484 case kEventAppActivated
:
487 wxTheApp
->SetActive( true , NULL
) ;
491 case kEventAppDeactivated
:
494 wxTheApp
->SetActive( false , NULL
) ;
504 pascal OSStatus
wxMacAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
506 EventRef formerEvent
= (EventRef
) wxTheApp
->MacGetCurrentEvent() ;
507 EventHandlerCallRef formerEventHandlerCallRef
= (EventHandlerCallRef
) wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
508 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
510 OSStatus result
= eventNotHandledErr
;
511 switch( GetEventClass( event
) )
513 case kEventClassCommand
:
514 result
= wxMacAppCommandEventHandler( handler
, event
, data
) ;
516 case kEventClassApplication
:
517 result
= wxMacAppApplicationEventHandler( handler
, event
, data
) ;
519 case kEventClassMenu
:
520 result
= wxMacAppMenuEventHandler( handler
, event
, data
) ;
522 case kEventClassMouse
:
524 wxMacCarbonEvent
cEvent( event
) ;
527 Point screenMouseLocation
= cEvent
.GetParameter
<Point
>(kEventParamMouseLocation
) ;
528 short windowPart
= ::FindWindow(screenMouseLocation
, &window
);
529 // only send this event in case it had not already been sent to a tlw, as we get
530 // double events otherwise (in case event.skip) was called
531 if ( window
== NULL
)
532 result
= wxMacTopLevelMouseEventHandler( handler
, event
, NULL
) ;
535 case kEventClassAppleEvent
:
538 wxMacConvertEventToRecord( event
, &rec
) ;
539 result
= AEProcessAppleEvent( &rec
) ;
546 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerEventHandlerCallRef
) ;
551 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler
)
553 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
554 // we know it's there ;-)
555 WXIMPORT
char std::__throws_bad_alloc
;
558 pascal static void wxMacAssertOutputHandler(OSType componentSignature
, UInt32 options
,
559 const char *assertionString
, const char *exceptionLabelString
,
560 const char *errorString
, const char *fileName
, long lineNumber
, void *value
, ConstStr255Param outputMsg
)
562 // flow into assert handling
563 wxString fileNameStr
;
564 wxString assertionStr
;
565 wxString exceptionStr
;
568 fileNameStr
= wxString(fileName
, wxConvLocal
);
569 assertionStr
= wxString(assertionString
, wxConvLocal
);
570 exceptionStr
= wxString((exceptionLabelString
!=0) ? exceptionLabelString
: "", wxConvLocal
) ;
571 errorStr
= wxString((errorString
!=0) ? errorString
: "", wxConvLocal
) ;
573 fileNameStr
= fileName
;
574 assertionStr
= assertionString
;
575 exceptionStr
= (exceptionLabelString
!=0) ? exceptionLabelString
: "" ;
576 errorStr
= (errorString
!=0) ? errorString
: "" ;
581 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
582 assertionStr
.c_str() ,
583 exceptionStr
.c_str() ,
585 fileNameStr
.c_str(), lineNumber
,
589 wxOnAssert(fileNameStr
, lineNumber
, assertionStr
,
590 wxString::Format( wxT("%s %s value (%p)") ,exceptionStr
, errorStr
, value
) ) ;
594 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
599 InstallDebugAssertOutputHandler ( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler
) );
601 UMAInitToolbox( 4, sm_isEmbedded
) ;
602 SetEventMask( everyEvent
) ;
603 UMAShowWatchCursor() ;
606 # if __option(profile)
607 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
612 // now avoid exceptions thrown for new (bad_alloc)
613 // FIXME CS for some changes outside wxMac does not compile anymore
615 std::__throws_bad_alloc
= 0 ;
620 s_macCursorRgn
= ::NewRgn() ;
622 // Mac OS X passes a process serial number command line argument when
623 // the application is launched from the Finder. This argument must be
624 // removed from the command line arguments before being handled by the
625 // application (otherwise applications would need to handle it)
628 static const wxChar
*ARG_PSN
= _T("-psn_");
629 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
631 // remove this argument
633 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
637 if ( !wxAppBase::Initialize(argc
, argv
) )
641 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
644 #if TARGET_API_MAC_OSX
645 // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
646 wxString startupCwd
= wxGetCwd() ;
647 if ( startupCwd
== wxT("/") || startupCwd
.Right(15) == wxT("/Contents/MacOS") )
649 CFURLRef url
= CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
650 CFURLRef urlParent
= CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault
, url
) ;
652 CFStringRef path
= CFURLCopyFileSystemPath ( urlParent
, kCFURLPOSIXPathStyle
) ;
653 CFRelease( urlParent
) ;
654 wxString cwd
= wxMacCFStringHolder(path
).AsString(wxLocale::GetSystemEncoding());
655 wxSetWorkingDirectory( cwd
) ;
659 wxMacCreateNotifierTable() ;
661 UMAShowArrowCursor() ;
666 bool wxApp::OnInitGui()
668 if( !wxAppBase::OnInitGui() )
671 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
675 InstallApplicationEventHandler(
676 GetwxMacAppEventHandlerUPP(),
677 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
682 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
683 NewAEEventHandlerUPP(AEHandleODoc
) ,
685 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
686 NewAEEventHandlerUPP(AEHandleOApp
) ,
688 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
689 NewAEEventHandlerUPP(AEHandlePDoc
) ,
691 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
692 NewAEEventHandlerUPP(AEHandleRApp
) ,
694 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
695 NewAEEventHandlerUPP(AEHandleQuit
) ,
702 void wxApp::CleanUp()
704 wxToolTip::RemoveToolTips() ;
706 // One last chance for pending objects to be cleaned up
707 wxTheApp
->DeletePendingObjects();
709 wxMacDestroyNotifierTable() ;
712 # if __option(profile)
713 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
718 UMACleanupToolbox() ;
719 if (s_macCursorRgn
) {
720 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
727 wxAppBase::CleanUp();
730 //----------------------------------------------------------------------
731 // misc initialization stuff
732 //----------------------------------------------------------------------
734 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
736 // for shared libraries we have to manually get the correct resource
737 // ref num upon initializing and releasing when terminating, therefore
738 // the __wxinitialize and __wxterminate must be used
741 void __sinit(void); /* (generated by linker) */
742 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
743 pascal void __terminate(void);
746 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
748 return __initialize( theInitBlock
) ;
751 pascal void __wxterminate(void)
756 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
758 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
760 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
761 OSStatus err
= noErr
;
764 switch( GetEventClass( event
) )
766 case kEventClassKeyboard
:
769 switch( GetEventKind(event
) )
771 case kEventRawKeyDown
:
772 rec
->what
= keyDown
;
774 case kEventRawKeyRepeat
:
775 rec
->what
= autoKey
;
777 case kEventRawKeyUp
:
780 case kEventRawKeyModifiersChanged
:
781 rec
->what
= nullEvent
;
790 unsigned char charCode
;
792 GetMouse( &rec
->where
) ;
794 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
795 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
796 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
797 rec
->modifiers
= modifiers
;
798 rec
->message
= (keyCode
<< 8 ) + charCode
;
802 case kEventClassTextInput
:
804 switch( GetEventKind( event
) )
806 case kEventTextInputUnicodeForKeyEvent
:
809 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
813 unsigned char charCode
;
815 GetMouse( &rec
->where
) ;
816 rec
->what
= keyDown
;
817 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
818 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
819 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
820 rec
->modifiers
= modifiers
;
821 rec
->message
= (keyCode
<< 8 ) + charCode
;
838 m_printMode
= wxPRINT_WINDOWS
;
840 m_macCurrentEvent
= NULL
;
841 m_macCurrentEventHandlerCallRef
= NULL
;
844 int wxApp::MainLoop()
848 RunApplicationEventLoop() ;
858 void wxApp::ExitMainLoop()
862 QuitApplicationEventLoop() ;
866 // Is a message/event pending?
867 bool wxApp::Pending()
869 // without the receive event (with pull param = false ) nothing is ever reported
871 ReceiveNextEvent (0, NULL
, kEventDurationNoWait
, false, &theEvent
);
872 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
875 // Dispatch a message.
876 bool wxApp::Dispatch()
883 void wxApp::OnIdle(wxIdleEvent
& event
)
885 wxAppBase::OnIdle(event
);
887 // If they are pending events, we must process them: pending events are
888 // either events to the threads other than main or events posted with
889 // wxPostEvent() functions
890 wxMacProcessNotifierAndPendingEvents();
892 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
893 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
896 void wxApp::WakeUpIdle()
907 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
910 GetTopWindow()->Close(TRUE
);
913 // Default behaviour: close the application with prompts. The
914 // user can veto the close, and therefore the end session.
915 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
919 if (!GetTopWindow()->Close(!event
.CanVeto()))
924 extern "C" void wxCYield() ;
930 // Yield to other processes
932 bool wxApp::Yield(bool onlyIfNeeded
)
938 wxFAIL_MSG( wxT("wxYield called recursively" ) );
946 // by definition yield should handle all non-processed events
950 OSStatus status
= noErr
;
953 s_inReceiveEvent
= true ;
954 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
955 s_inReceiveEvent
= false ;
957 if ( status
== eventLoopTimedOutErr
)
959 // make sure next time the event loop will trigger idle events
960 sleepTime
= kEventDurationNoWait
;
962 else if ( status
== eventLoopQuitErr
)
964 // according to QA1061 this may also occur when a WakeUp Process
969 MacHandleOneEvent( theEvent
) ;
970 ReleaseEvent(theEvent
);
972 } while( status
== noErr
) ;
974 wxMacProcessNotifierAndPendingEvents() ;
980 void wxApp::MacDoOneEvent()
984 s_inReceiveEvent
= true ;
985 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
986 s_inReceiveEvent
= false ;
987 if ( status
== eventLoopTimedOutErr
)
989 if ( wxTheApp
->ProcessIdle() )
990 sleepTime
= kEventDurationNoWait
;
992 sleepTime
= kEventDurationSecond
;
994 else if ( status
== eventLoopQuitErr
)
996 // according to QA1061 this may also occur when a WakeUp Process
1001 MacHandleOneEvent( theEvent
) ;
1002 ReleaseEvent(theEvent
);
1003 sleepTime
= kEventDurationNoWait
;
1007 DeletePendingObjects() ;
1008 wxMacProcessNotifierAndPendingEvents() ;
1011 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1013 // Override to process unhandled events as you please
1016 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1018 EventTargetRef theTarget
;
1019 theTarget
= GetEventDispatcherTarget();
1020 m_macCurrentEvent
= evr
;
1021 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1022 if(status
== eventNotHandledErr
)
1024 MacHandleUnhandledEvent(evr
);
1026 wxMacProcessNotifierAndPendingEvents() ;
1028 wxMutexGuiLeaveOrEnter();
1029 #endif // wxUSE_THREADS
1032 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1033 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1038 case kHomeCharCode
:
1041 case kEnterCharCode
:
1042 retval
= WXK_RETURN
;
1047 case kHelpCharCode
:
1050 case kBackspaceCharCode
:
1056 case kPageUpCharCode
:
1057 retval
= WXK_PAGEUP
;
1059 case kPageDownCharCode
:
1060 retval
= WXK_PAGEDOWN
;
1062 case kReturnCharCode
:
1063 retval
= WXK_RETURN
;
1065 case kFunctionKeyCharCode
:
1117 case kEscapeCharCode
:
1118 retval
= WXK_ESCAPE
;
1120 case kLeftArrowCharCode
:
1123 case kRightArrowCharCode
:
1124 retval
= WXK_RIGHT
;
1126 case kUpArrowCharCode
:
1129 case kDownArrowCharCode
:
1132 case kDeleteCharCode
:
1133 retval
= WXK_DELETE
;
1141 int wxMacKeyCodeToModifier(wxKeyCode key
)
1166 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1169 // wxHIDKeyboard keyboard;
1170 // return keyboard.IsActive(key);
1172 // TODO: Have it use HID Manager on OSX...
1173 //if OS X > 10.2 (i.e. 10.2.x)
1174 //a known apple bug prevents the system from determining led
1175 //states with GetKeys... can only determine caps lock led
1176 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key
));
1178 // KeyMapByteArray keymap;
1179 // GetKeys((BigEndianLong*)keymap);
1180 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1185 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1192 keychar
= short(keymessage
& charCodeMask
);
1193 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1195 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1197 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1198 // and look at the character after
1200 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1201 keychar
= short(keyInfo
& charCodeMask
);
1202 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1204 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1205 long realkeyval
= keyval
;
1206 if ( keyval
== keychar
)
1208 // 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)
1209 realkeyval
= short(keymessage
& charCodeMask
) ;
1210 keyval
= wxToupper( keyval
) ;
1213 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1214 bool handled
= false ;
1215 event
.m_shiftDown
= modifiers
& shiftKey
;
1216 event
.m_controlDown
= modifiers
& controlKey
;
1217 event
.m_altDown
= modifiers
& optionKey
;
1218 event
.m_metaDown
= modifiers
& cmdKey
;
1219 event
.m_keyCode
= keyval
;
1223 event
.m_timeStamp
= when
;
1224 event
.SetEventObject(focus
);
1225 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1226 if ( handled
&& event
.GetSkipped() )
1233 wxWindow
*ancestor
= focus
;
1236 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1239 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1240 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1243 if (ancestor
->IsTopLevel())
1245 ancestor
= ancestor
->GetParent();
1248 #endif // wxUSE_ACCEL
1252 event
.Skip( FALSE
) ;
1253 event
.SetEventType( wxEVT_CHAR
) ;
1255 event
.m_keyCode
= realkeyval
;
1257 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1258 if ( handled
&& event
.GetSkipped() )
1261 if ( !handled
&& (keyval
== WXK_TAB
) )
1263 wxWindow
* iter
= focus
->GetParent() ;
1264 while( iter
&& !handled
)
1266 if ( iter
->HasFlag( wxTAB_TRAVERSAL
) )
1268 wxNavigationKeyEvent new_event
;
1269 new_event
.SetEventObject( focus
);
1270 new_event
.SetDirection( !event
.ShiftDown() );
1271 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1272 new_event
.SetWindowChange( event
.ControlDown() );
1273 new_event
.SetCurrentFocus( focus
);
1274 handled
= focus
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
1275 if ( handled
&& new_event
.GetSkipped() )
1278 iter
= iter
->GetParent() ;
1281 // backdoor handler for default return and command escape
1282 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1284 // if window is not having a focus still testing for default enter or cancel
1285 // TODO add the UMA version for ActiveNonFloatingWindow
1286 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1289 if ( keyval
== WXK_RETURN
)
1291 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1293 if ( def
&& def
->IsEnabled() )
1295 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1296 event
.SetEventObject(def
);
1297 def
->Command(event
);
1301 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1302 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1304 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1305 new_event
.SetEventObject( focus
);
1306 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1313 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1320 keychar
= short(keymessage
& charCodeMask
);
1321 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1322 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1324 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1325 // and look at the character after
1327 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1328 keychar
= short(keyInfo
& charCodeMask
);
1329 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1331 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1333 if ( keyval
== keychar
)
1335 keyval
= wxToupper( keyval
) ;
1337 bool handled
= false ;
1339 wxKeyEvent
event(wxEVT_KEY_UP
);
1340 event
.m_shiftDown
= modifiers
& shiftKey
;
1341 event
.m_controlDown
= modifiers
& controlKey
;
1342 event
.m_altDown
= modifiers
& optionKey
;
1343 event
.m_metaDown
= modifiers
& cmdKey
;
1344 event
.m_keyCode
= keyval
;
1348 event
.m_timeStamp
= when
;
1349 event
.SetEventObject(focus
);
1350 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;