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
:
523 result
= wxMacTopLevelMouseEventHandler( handler
, event
, NULL
) ;
525 case kEventClassAppleEvent
:
528 wxMacConvertEventToRecord( event
, &rec
) ;
529 result
= AEProcessAppleEvent( &rec
) ;
536 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerEventHandlerCallRef
) ;
541 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler
)
543 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
544 // we know it's there ;-)
545 WXIMPORT
char std::__throws_bad_alloc
;
548 pascal static void wxMacAssertOutputHandler(OSType componentSignature
, UInt32 options
,
549 const char *assertionString
, const char *exceptionLabelString
,
550 const char *errorString
, const char *fileName
, long lineNumber
, void *value
, ConstStr255Param outputMsg
)
552 // flow into assert handling
553 wxString fileNameStr
;
554 wxString assertionStr
;
555 wxString exceptionStr
;
558 fileNameStr
= wxString(fileName
, wxConvLocal
);
559 assertionStr
= wxString(assertionString
, wxConvLocal
);
560 exceptionStr
= wxString((exceptionLabelString
!=0) ? exceptionLabelString
: "", wxConvLocal
) ;
561 errorStr
= wxString((errorString
!=0) ? errorString
: "", wxConvLocal
) ;
563 fileNameStr
= fileName
;
564 assertionStr
= assertionString
;
565 exceptionStr
= (exceptionLabelString
!=0) ? exceptionLabelString
: "" ;
566 errorStr
= (errorString
!=0) ? errorString
: "" ;
571 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
572 assertionStr
.c_str() ,
573 exceptionStr
.c_str() ,
575 fileNameStr
.c_str(), lineNumber
,
579 wxOnAssert(fileNameStr
, lineNumber
, assertionStr
,
580 wxString::Format( wxT("%s %s value (%p)") ,exceptionStr
, errorStr
, value
) ) ;
584 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
589 InstallDebugAssertOutputHandler ( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler
) );
591 UMAInitToolbox( 4, sm_isEmbedded
) ;
592 SetEventMask( everyEvent
) ;
593 UMAShowWatchCursor() ;
595 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
596 // open shared library resources from here since we don't have
597 // __wxinitialize in Mach-O shared libraries
598 wxStAppResource::OpenSharedLibraryResource(NULL
);
602 # if __option(profile)
603 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
608 // now avoid exceptions thrown for new (bad_alloc)
609 // FIXME CS for some changes outside wxMac does not compile anymore
611 std::__throws_bad_alloc
= 0 ;
616 s_macCursorRgn
= ::NewRgn() ;
618 // Mac OS X passes a process serial number command line argument when
619 // the application is launched from the Finder. This argument must be
620 // removed from the command line arguments before being handled by the
621 // application (otherwise applications would need to handle it)
624 static const wxChar
*ARG_PSN
= _T("-psn_");
625 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
627 // remove this argument
629 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
633 if ( !wxAppBase::Initialize(argc
, argv
) )
637 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
640 #if TARGET_API_MAC_OSX
641 // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
642 wxString startupCwd
= wxGetCwd() ;
643 if ( startupCwd
== wxT("/") || startupCwd
.Right(15) == wxT("/Contents/MacOS") )
645 CFURLRef url
= CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
646 CFURLRef urlParent
= CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault
, url
) ;
648 CFStringRef path
= CFURLCopyFileSystemPath ( urlParent
, kCFURLPOSIXPathStyle
) ;
649 CFRelease( urlParent
) ;
650 wxString cwd
= wxMacCFStringHolder(path
).AsString(wxLocale::GetSystemEncoding());
651 wxSetWorkingDirectory( cwd
) ;
655 wxMacCreateNotifierTable() ;
657 UMAShowArrowCursor() ;
662 bool wxApp::OnInitGui()
664 if( !wxAppBase::OnInitGui() )
667 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
671 InstallApplicationEventHandler(
672 GetwxMacAppEventHandlerUPP(),
673 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
678 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
679 NewAEEventHandlerUPP(AEHandleODoc
) ,
681 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
682 NewAEEventHandlerUPP(AEHandleOApp
) ,
684 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
685 NewAEEventHandlerUPP(AEHandlePDoc
) ,
687 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
688 NewAEEventHandlerUPP(AEHandleRApp
) ,
690 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
691 NewAEEventHandlerUPP(AEHandleQuit
) ,
698 void wxApp::CleanUp()
700 wxToolTip::RemoveToolTips() ;
702 // One last chance for pending objects to be cleaned up
703 wxTheApp
->DeletePendingObjects();
705 wxMacDestroyNotifierTable() ;
708 # if __option(profile)
709 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
714 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
715 // close shared library resources from here since we don't have
716 // __wxterminate in Mach-O shared libraries
717 wxStAppResource::CloseSharedLibraryResource();
720 UMACleanupToolbox() ;
721 if (s_macCursorRgn
) {
722 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
729 wxAppBase::CleanUp();
732 //----------------------------------------------------------------------
733 // misc initialization stuff
734 //----------------------------------------------------------------------
736 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
738 // for shared libraries we have to manually get the correct resource
739 // ref num upon initializing and releasing when terminating, therefore
740 // the __wxinitialize and __wxterminate must be used
743 void __sinit(void); /* (generated by linker) */
744 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
745 pascal void __terminate(void);
748 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
750 return __initialize( theInitBlock
) ;
753 pascal void __wxterminate(void)
758 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
760 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
762 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
763 OSStatus err
= noErr
;
766 switch( GetEventClass( event
) )
768 case kEventClassKeyboard
:
771 switch( GetEventKind(event
) )
773 case kEventRawKeyDown
:
774 rec
->what
= keyDown
;
776 case kEventRawKeyRepeat
:
777 rec
->what
= autoKey
;
779 case kEventRawKeyUp
:
782 case kEventRawKeyModifiersChanged
:
783 rec
->what
= nullEvent
;
792 unsigned char charCode
;
794 GetMouse( &rec
->where
) ;
796 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
797 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
798 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
799 rec
->modifiers
= modifiers
;
800 rec
->message
= (keyCode
<< 8 ) + charCode
;
804 case kEventClassTextInput
:
806 switch( GetEventKind( event
) )
808 case kEventTextInputUnicodeForKeyEvent
:
811 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
815 unsigned char charCode
;
817 GetMouse( &rec
->where
) ;
818 rec
->what
= keyDown
;
819 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
820 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
821 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
822 rec
->modifiers
= modifiers
;
823 rec
->message
= (keyCode
<< 8 ) + charCode
;
840 m_printMode
= wxPRINT_WINDOWS
;
842 m_macCurrentEvent
= NULL
;
843 m_macCurrentEventHandlerCallRef
= NULL
;
846 int wxApp::MainLoop()
850 RunApplicationEventLoop() ;
860 void wxApp::ExitMainLoop()
864 QuitApplicationEventLoop() ;
868 // Is a message/event pending?
869 bool wxApp::Pending()
871 // without the receive event (with pull param = false ) nothing is ever reported
873 ReceiveNextEvent (0, NULL
, kEventDurationNoWait
, false, &theEvent
);
874 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
877 // Dispatch a message.
878 bool wxApp::Dispatch()
885 void wxApp::OnIdle(wxIdleEvent
& event
)
887 wxAppBase::OnIdle(event
);
889 // If they are pending events, we must process them: pending events are
890 // either events to the threads other than main or events posted with
891 // wxPostEvent() functions
892 wxMacProcessNotifierAndPendingEvents();
894 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
895 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
898 void wxApp::WakeUpIdle()
909 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
912 GetTopWindow()->Close(TRUE
);
915 // Default behaviour: close the application with prompts. The
916 // user can veto the close, and therefore the end session.
917 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
921 if (!GetTopWindow()->Close(!event
.CanVeto()))
926 extern "C" void wxCYield() ;
932 // Yield to other processes
934 bool wxApp::Yield(bool onlyIfNeeded
)
940 wxFAIL_MSG( wxT("wxYield called recursively" ) );
948 // by definition yield should handle all non-processed events
952 OSStatus status
= noErr
;
955 s_inReceiveEvent
= true ;
956 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
957 s_inReceiveEvent
= false ;
959 if ( status
== eventLoopTimedOutErr
)
961 // make sure next time the event loop will trigger idle events
962 sleepTime
= kEventDurationNoWait
;
964 else if ( status
== eventLoopQuitErr
)
966 // according to QA1061 this may also occur when a WakeUp Process
971 MacHandleOneEvent( theEvent
) ;
972 ReleaseEvent(theEvent
);
974 } while( status
== noErr
) ;
976 wxMacProcessNotifierAndPendingEvents() ;
982 void wxApp::MacDoOneEvent()
986 s_inReceiveEvent
= true ;
987 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
988 s_inReceiveEvent
= false ;
989 if ( status
== eventLoopTimedOutErr
)
991 if ( wxTheApp
->ProcessIdle() )
992 sleepTime
= kEventDurationNoWait
;
994 sleepTime
= kEventDurationSecond
;
996 else if ( status
== eventLoopQuitErr
)
998 // according to QA1061 this may also occur when a WakeUp Process
1003 MacHandleOneEvent( theEvent
) ;
1004 ReleaseEvent(theEvent
);
1005 sleepTime
= kEventDurationNoWait
;
1009 DeletePendingObjects() ;
1010 wxMacProcessNotifierAndPendingEvents() ;
1013 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1015 // Override to process unhandled events as you please
1018 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1020 EventTargetRef theTarget
;
1021 theTarget
= GetEventDispatcherTarget();
1022 m_macCurrentEvent
= evr
;
1023 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1024 if(status
== eventNotHandledErr
)
1026 MacHandleUnhandledEvent(evr
);
1028 wxMacProcessNotifierAndPendingEvents() ;
1030 wxMutexGuiLeaveOrEnter();
1031 #endif // wxUSE_THREADS
1034 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1035 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1040 case kHomeCharCode
:
1043 case kEnterCharCode
:
1044 retval
= WXK_RETURN
;
1049 case kHelpCharCode
:
1052 case kBackspaceCharCode
:
1058 case kPageUpCharCode
:
1059 retval
= WXK_PAGEUP
;
1061 case kPageDownCharCode
:
1062 retval
= WXK_PAGEDOWN
;
1064 case kReturnCharCode
:
1065 retval
= WXK_RETURN
;
1067 case kFunctionKeyCharCode
:
1119 case kEscapeCharCode
:
1120 retval
= WXK_ESCAPE
;
1122 case kLeftArrowCharCode
:
1125 case kRightArrowCharCode
:
1126 retval
= WXK_RIGHT
;
1128 case kUpArrowCharCode
:
1131 case kDownArrowCharCode
:
1134 case kDeleteCharCode
:
1135 retval
= WXK_DELETE
;
1143 int wxMacKeyCodeToModifier(wxKeyCode key
)
1168 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1171 // wxHIDKeyboard keyboard;
1172 // return keyboard.IsActive(key);
1174 // TODO: Have it use HID Manager on OSX...
1175 //if OS X > 10.2 (i.e. 10.2.x)
1176 //a known apple bug prevents the system from determining led
1177 //states with GetKeys... can only determine caps lock led
1178 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key
));
1180 // KeyMapByteArray keymap;
1181 // GetKeys((BigEndianLong*)keymap);
1182 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1187 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1194 keychar
= short(keymessage
& charCodeMask
);
1195 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1197 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1199 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1200 // and look at the character after
1202 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1203 keychar
= short(keyInfo
& charCodeMask
);
1204 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1206 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1207 long realkeyval
= keyval
;
1208 if ( keyval
== keychar
)
1210 // 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)
1211 realkeyval
= short(keymessage
& charCodeMask
) ;
1212 keyval
= wxToupper( keyval
) ;
1215 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1216 bool handled
= false ;
1217 event
.m_shiftDown
= modifiers
& shiftKey
;
1218 event
.m_controlDown
= modifiers
& controlKey
;
1219 event
.m_altDown
= modifiers
& optionKey
;
1220 event
.m_metaDown
= modifiers
& cmdKey
;
1221 event
.m_keyCode
= keyval
;
1225 event
.m_timeStamp
= when
;
1226 event
.SetEventObject(focus
);
1227 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1228 if ( handled
&& event
.GetSkipped() )
1235 wxWindow
*ancestor
= focus
;
1238 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1241 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1242 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1245 if (ancestor
->IsTopLevel())
1247 ancestor
= ancestor
->GetParent();
1250 #endif // wxUSE_ACCEL
1254 event
.Skip( FALSE
) ;
1255 event
.SetEventType( wxEVT_CHAR
) ;
1257 event
.m_keyCode
= realkeyval
;
1259 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1260 if ( handled
&& event
.GetSkipped() )
1263 if ( !handled
&& (keyval
== WXK_TAB
) )
1265 wxWindow
* iter
= focus
->GetParent() ;
1266 while( iter
&& !handled
)
1268 if ( iter
->HasFlag( wxTAB_TRAVERSAL
) )
1270 wxNavigationKeyEvent new_event
;
1271 new_event
.SetEventObject( focus
);
1272 new_event
.SetDirection( !event
.ShiftDown() );
1273 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1274 new_event
.SetWindowChange( event
.ControlDown() );
1275 new_event
.SetCurrentFocus( focus
);
1276 handled
= focus
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
1277 if ( handled
&& new_event
.GetSkipped() )
1280 iter
= iter
->GetParent() ;
1283 // backdoor handler for default return and command escape
1284 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1286 // if window is not having a focus still testing for default enter or cancel
1287 // TODO add the UMA version for ActiveNonFloatingWindow
1288 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1291 if ( keyval
== WXK_RETURN
)
1293 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1295 if ( def
&& def
->IsEnabled() )
1297 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1298 event
.SetEventObject(def
);
1299 def
->Command(event
);
1303 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1304 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1306 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1307 new_event
.SetEventObject( focus
);
1308 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1315 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1322 keychar
= short(keymessage
& charCodeMask
);
1323 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1324 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1326 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1327 // and look at the character after
1329 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1330 keychar
= short(keyInfo
& charCodeMask
);
1331 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1333 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1335 if ( keyval
== keychar
)
1337 keyval
= wxToupper( keyval
) ;
1339 bool handled
= false ;
1341 wxKeyEvent
event(wxEVT_KEY_UP
);
1342 event
.m_shiftDown
= modifiers
& shiftKey
;
1343 event
.m_controlDown
= modifiers
& controlKey
;
1344 event
.m_altDown
= modifiers
& optionKey
;
1345 event
.m_metaDown
= modifiers
& cmdKey
;
1346 event
.m_keyCode
= keyval
;
1350 event
.m_timeStamp
= when
;
1351 event
.SetEventObject(focus
);
1352 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;