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
) ) ;
610 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
615 InstallDebugAssertOutputHandler ( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler
) );
617 UMAInitToolbox( 4, sm_isEmbedded
) ;
618 SetEventMask( everyEvent
) ;
619 UMAShowWatchCursor() ;
622 # if __option(profile)
623 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
628 // now avoid exceptions thrown for new (bad_alloc)
629 // FIXME CS for some changes outside wxMac does not compile anymore
631 std::__throws_bad_alloc
= 0 ;
636 s_macCursorRgn
= ::NewRgn() ;
638 // Mac OS X passes a process serial number command line argument when
639 // the application is launched from the Finder. This argument must be
640 // removed from the command line arguments before being handled by the
641 // application (otherwise applications would need to handle it)
644 static const wxChar
*ARG_PSN
= _T("-psn_");
645 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
647 // remove this argument
649 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
653 if ( !wxAppBase::Initialize(argc
, argv
) )
657 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
660 #if TARGET_API_MAC_OSX
661 // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
662 wxString startupCwd
= wxGetCwd() ;
663 if ( startupCwd
== wxT("/") || startupCwd
.Right(15) == wxT("/Contents/MacOS") )
665 CFURLRef url
= CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
666 CFURLRef urlParent
= CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault
, url
) ;
668 CFStringRef path
= CFURLCopyFileSystemPath ( urlParent
, kCFURLPOSIXPathStyle
) ;
669 CFRelease( urlParent
) ;
670 wxString cwd
= wxMacCFStringHolder(path
).AsString(wxLocale::GetSystemEncoding());
671 wxSetWorkingDirectory( cwd
) ;
675 wxMacCreateNotifierTable() ;
677 UMAShowArrowCursor() ;
682 bool wxApp::OnInitGui()
684 if( !wxAppBase::OnInitGui() )
687 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
691 InstallApplicationEventHandler(
692 GetwxMacAppEventHandlerUPP(),
693 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
698 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
699 NewAEEventHandlerUPP(AEHandleODoc
) ,
701 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
702 NewAEEventHandlerUPP(AEHandleOApp
) ,
704 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
705 NewAEEventHandlerUPP(AEHandlePDoc
) ,
707 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
708 NewAEEventHandlerUPP(AEHandleRApp
) ,
710 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
711 NewAEEventHandlerUPP(AEHandleQuit
) ,
718 void wxApp::CleanUp()
720 wxToolTip::RemoveToolTips() ;
722 // One last chance for pending objects to be cleaned up
723 wxTheApp
->DeletePendingObjects();
725 wxMacDestroyNotifierTable() ;
728 # if __option(profile)
729 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
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
)
1183 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1185 //if OS X > 10.2 (i.e. 10.2.x)
1186 //a known apple bug prevents the system from determining led
1187 //states with GetKeys... can only determine caps lock led
1188 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key
));
1190 // KeyMapByteArray keymap;
1191 // GetKeys((BigEndianLong*)keymap);
1192 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1197 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1204 keychar
= short(keymessage
& charCodeMask
);
1205 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1207 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1209 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1210 // and look at the character after
1212 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1213 keychar
= short(keyInfo
& charCodeMask
);
1214 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1216 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1217 long realkeyval
= keyval
;
1218 if ( keyval
== keychar
)
1220 // 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)
1221 realkeyval
= short(keymessage
& charCodeMask
) ;
1222 keyval
= wxToupper( keyval
) ;
1225 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1226 bool handled
= false ;
1227 event
.m_shiftDown
= modifiers
& shiftKey
;
1228 event
.m_controlDown
= modifiers
& controlKey
;
1229 event
.m_altDown
= modifiers
& optionKey
;
1230 event
.m_metaDown
= modifiers
& cmdKey
;
1231 event
.m_keyCode
= keyval
;
1235 event
.SetTimestamp(when
);
1236 event
.SetEventObject(focus
);
1237 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1238 if ( handled
&& event
.GetSkipped() )
1245 wxWindow
*ancestor
= focus
;
1248 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1251 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1252 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1255 if (ancestor
->IsTopLevel())
1257 ancestor
= ancestor
->GetParent();
1260 #endif // wxUSE_ACCEL
1264 event
.Skip( FALSE
) ;
1265 event
.SetEventType( wxEVT_CHAR
) ;
1267 event
.m_keyCode
= realkeyval
;
1269 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1270 if ( handled
&& event
.GetSkipped() )
1273 if ( !handled
&& (keyval
== WXK_TAB
) )
1275 wxWindow
* iter
= focus
->GetParent() ;
1276 while( iter
&& !handled
)
1278 if ( iter
->HasFlag( wxTAB_TRAVERSAL
) )
1280 wxNavigationKeyEvent new_event
;
1281 new_event
.SetEventObject( focus
);
1282 new_event
.SetDirection( !event
.ShiftDown() );
1283 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1284 new_event
.SetWindowChange( event
.ControlDown() );
1285 new_event
.SetCurrentFocus( focus
);
1286 handled
= focus
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
1287 if ( handled
&& new_event
.GetSkipped() )
1290 iter
= iter
->GetParent() ;
1293 // backdoor handler for default return and command escape
1294 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1296 // if window is not having a focus still testing for default enter or cancel
1297 // TODO add the UMA version for ActiveNonFloatingWindow
1298 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1301 if ( keyval
== WXK_RETURN
)
1303 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1305 if ( def
&& def
->IsEnabled() )
1307 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1308 event
.SetEventObject(def
);
1309 def
->Command(event
);
1313 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1314 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1316 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1317 new_event
.SetEventObject( focus
);
1318 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1325 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1332 keychar
= short(keymessage
& charCodeMask
);
1333 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1334 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1336 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1337 // and look at the character after
1339 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1340 keychar
= short(keyInfo
& charCodeMask
);
1341 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1343 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1345 if ( keyval
== keychar
)
1347 keyval
= wxToupper( keyval
) ;
1349 bool handled
= false ;
1351 wxKeyEvent
event(wxEVT_KEY_UP
);
1352 event
.m_shiftDown
= modifiers
& shiftKey
;
1353 event
.m_controlDown
= modifiers
& controlKey
;
1354 event
.m_altDown
= modifiers
& optionKey
;
1355 event
.m_metaDown
= modifiers
& cmdKey
;
1356 event
.m_keyCode
= keyval
;
1360 event
.SetTimestamp(when
);
1361 event
.SetEventObject(focus
);
1362 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;