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()
721 wxToolTip::RemoveToolTips() ;
724 // One last chance for pending objects to be cleaned up
725 wxTheApp
->DeletePendingObjects();
727 wxMacDestroyNotifierTable() ;
730 # if __option(profile)
731 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
736 UMACleanupToolbox() ;
737 if (s_macCursorRgn
) {
738 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
745 wxAppBase::CleanUp();
748 //----------------------------------------------------------------------
749 // misc initialization stuff
750 //----------------------------------------------------------------------
752 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
754 // for shared libraries we have to manually get the correct resource
755 // ref num upon initializing and releasing when terminating, therefore
756 // the __wxinitialize and __wxterminate must be used
759 void __sinit(void); /* (generated by linker) */
760 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
761 pascal void __terminate(void);
764 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
766 return __initialize( theInitBlock
) ;
769 pascal void __wxterminate(void)
774 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
776 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
778 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
779 OSStatus err
= noErr
;
782 switch( GetEventClass( event
) )
784 case kEventClassKeyboard
:
787 switch( GetEventKind(event
) )
789 case kEventRawKeyDown
:
790 rec
->what
= keyDown
;
792 case kEventRawKeyRepeat
:
793 rec
->what
= autoKey
;
795 case kEventRawKeyUp
:
798 case kEventRawKeyModifiersChanged
:
799 rec
->what
= nullEvent
;
808 unsigned char charCode
;
810 GetMouse( &rec
->where
) ;
812 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
813 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
814 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
815 rec
->modifiers
= modifiers
;
816 rec
->message
= (keyCode
<< 8 ) + charCode
;
820 case kEventClassTextInput
:
822 switch( GetEventKind( event
) )
824 case kEventTextInputUnicodeForKeyEvent
:
827 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
831 unsigned char charCode
;
833 GetMouse( &rec
->where
) ;
834 rec
->what
= keyDown
;
835 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
836 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
837 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
838 rec
->modifiers
= modifiers
;
839 rec
->message
= (keyCode
<< 8 ) + charCode
;
856 m_printMode
= wxPRINT_WINDOWS
;
858 m_macCurrentEvent
= NULL
;
859 m_macCurrentEventHandlerCallRef
= NULL
;
862 int wxApp::MainLoop()
866 RunApplicationEventLoop() ;
876 void wxApp::ExitMainLoop()
880 QuitApplicationEventLoop() ;
884 // Is a message/event pending?
885 bool wxApp::Pending()
887 // without the receive event (with pull param = false ) nothing is ever reported
889 ReceiveNextEvent (0, NULL
, kEventDurationNoWait
, false, &theEvent
);
890 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
893 // Dispatch a message.
894 bool wxApp::Dispatch()
901 void wxApp::OnIdle(wxIdleEvent
& event
)
903 wxAppBase::OnIdle(event
);
905 // If they are pending events, we must process them: pending events are
906 // either events to the threads other than main or events posted with
907 // wxPostEvent() functions
908 wxMacProcessNotifierAndPendingEvents();
910 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
911 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
914 void wxApp::WakeUpIdle()
925 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
928 GetTopWindow()->Close(TRUE
);
931 // Default behaviour: close the application with prompts. The
932 // user can veto the close, and therefore the end session.
933 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
937 if (!GetTopWindow()->Close(!event
.CanVeto()))
942 extern "C" void wxCYield() ;
948 // Yield to other processes
950 bool wxApp::Yield(bool onlyIfNeeded
)
956 wxFAIL_MSG( wxT("wxYield called recursively" ) );
964 // by definition yield should handle all non-processed events
968 OSStatus status
= noErr
;
971 s_inReceiveEvent
= true ;
972 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
973 s_inReceiveEvent
= false ;
975 if ( status
== eventLoopTimedOutErr
)
977 // make sure next time the event loop will trigger idle events
978 sleepTime
= kEventDurationNoWait
;
980 else if ( status
== eventLoopQuitErr
)
982 // according to QA1061 this may also occur when a WakeUp Process
987 MacHandleOneEvent( theEvent
) ;
988 ReleaseEvent(theEvent
);
990 } while( status
== noErr
) ;
992 wxMacProcessNotifierAndPendingEvents() ;
998 void wxApp::MacDoOneEvent()
1002 s_inReceiveEvent
= true ;
1003 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1004 s_inReceiveEvent
= false ;
1005 if ( status
== eventLoopTimedOutErr
)
1007 if ( wxTheApp
->ProcessIdle() )
1008 sleepTime
= kEventDurationNoWait
;
1010 sleepTime
= kEventDurationSecond
;
1012 else if ( status
== eventLoopQuitErr
)
1014 // according to QA1061 this may also occur when a WakeUp Process
1019 MacHandleOneEvent( theEvent
) ;
1020 ReleaseEvent(theEvent
);
1021 sleepTime
= kEventDurationNoWait
;
1025 DeletePendingObjects() ;
1026 wxMacProcessNotifierAndPendingEvents() ;
1029 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1031 // Override to process unhandled events as you please
1034 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1036 EventTargetRef theTarget
;
1037 theTarget
= GetEventDispatcherTarget();
1038 m_macCurrentEvent
= evr
;
1039 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1040 if(status
== eventNotHandledErr
)
1042 MacHandleUnhandledEvent(evr
);
1044 wxMacProcessNotifierAndPendingEvents() ;
1046 wxMutexGuiLeaveOrEnter();
1047 #endif // wxUSE_THREADS
1050 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1051 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1056 case kHomeCharCode
:
1059 case kEnterCharCode
:
1060 retval
= WXK_RETURN
;
1065 case kHelpCharCode
:
1068 case kBackspaceCharCode
:
1074 case kPageUpCharCode
:
1075 retval
= WXK_PAGEUP
;
1077 case kPageDownCharCode
:
1078 retval
= WXK_PAGEDOWN
;
1080 case kReturnCharCode
:
1081 retval
= WXK_RETURN
;
1083 case kFunctionKeyCharCode
:
1135 case kEscapeCharCode
:
1136 retval
= WXK_ESCAPE
;
1138 case kLeftArrowCharCode
:
1141 case kRightArrowCharCode
:
1142 retval
= WXK_RIGHT
;
1144 case kUpArrowCharCode
:
1147 case kDownArrowCharCode
:
1150 case kDeleteCharCode
:
1151 retval
= WXK_DELETE
;
1159 int wxMacKeyCodeToModifier(wxKeyCode key
)
1185 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1187 //if OS X > 10.2 (i.e. 10.2.x)
1188 //a known apple bug prevents the system from determining led
1189 //states with GetKeys... can only determine caps lock led
1190 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key
));
1192 // KeyMapByteArray keymap;
1193 // GetKeys((BigEndianLong*)keymap);
1194 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1199 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1206 keychar
= short(keymessage
& charCodeMask
);
1207 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1209 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1211 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1212 // and look at the character after
1214 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1215 keychar
= short(keyInfo
& charCodeMask
);
1216 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1218 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1219 long realkeyval
= keyval
;
1220 if ( keyval
== keychar
)
1222 // 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)
1223 realkeyval
= short(keymessage
& charCodeMask
) ;
1224 keyval
= wxToupper( keyval
) ;
1227 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1228 bool handled
= false ;
1229 event
.m_shiftDown
= modifiers
& shiftKey
;
1230 event
.m_controlDown
= modifiers
& controlKey
;
1231 event
.m_altDown
= modifiers
& optionKey
;
1232 event
.m_metaDown
= modifiers
& cmdKey
;
1233 event
.m_keyCode
= keyval
;
1237 event
.SetTimestamp(when
);
1238 event
.SetEventObject(focus
);
1239 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1240 if ( handled
&& event
.GetSkipped() )
1247 wxWindow
*ancestor
= focus
;
1250 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1253 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1254 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1257 if (ancestor
->IsTopLevel())
1259 ancestor
= ancestor
->GetParent();
1262 #endif // wxUSE_ACCEL
1266 wxTopLevelWindowMac
*tlw
= focus
->MacGetTopLevelWindow() ;
1270 event
.Skip( FALSE
) ;
1271 event
.SetEventType( wxEVT_CHAR_HOOK
);
1273 event
.m_keyCode
= realkeyval
;
1275 handled
= tlw
->GetEventHandler()->ProcessEvent( event
);
1276 if ( handled
&& event
.GetSkipped() )
1283 event
.Skip( FALSE
) ;
1284 event
.SetEventType( wxEVT_CHAR
) ;
1286 event
.m_keyCode
= realkeyval
;
1288 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1289 if ( handled
&& event
.GetSkipped() )
1292 if ( !handled
&& (keyval
== WXK_TAB
) )
1294 wxWindow
* iter
= focus
->GetParent() ;
1295 while( iter
&& !handled
)
1297 if ( iter
->HasFlag( wxTAB_TRAVERSAL
) )
1299 wxNavigationKeyEvent new_event
;
1300 new_event
.SetEventObject( focus
);
1301 new_event
.SetDirection( !event
.ShiftDown() );
1302 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1303 new_event
.SetWindowChange( event
.ControlDown() );
1304 new_event
.SetCurrentFocus( focus
);
1305 handled
= focus
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
1306 if ( handled
&& new_event
.GetSkipped() )
1309 iter
= iter
->GetParent() ;
1312 // backdoor handler for default return and command escape
1313 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1315 // if window is not having a focus still testing for default enter or cancel
1316 // TODO add the UMA version for ActiveNonFloatingWindow
1317 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1320 if ( keyval
== WXK_RETURN
)
1322 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1324 if ( def
&& def
->IsEnabled() )
1326 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1327 event
.SetEventObject(def
);
1328 def
->Command(event
);
1332 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1333 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1335 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1336 new_event
.SetEventObject( focus
);
1337 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1344 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1351 keychar
= short(keymessage
& charCodeMask
);
1352 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1353 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1355 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1356 // and look at the character after
1358 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1359 keychar
= short(keyInfo
& charCodeMask
);
1360 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1362 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1364 if ( keyval
== keychar
)
1366 keyval
= wxToupper( keyval
) ;
1368 bool handled
= false ;
1370 wxKeyEvent
event(wxEVT_KEY_UP
);
1371 event
.m_shiftDown
= modifiers
& shiftKey
;
1372 event
.m_controlDown
= modifiers
& controlKey
;
1373 event
.m_altDown
= modifiers
& optionKey
;
1374 event
.m_metaDown
= modifiers
& cmdKey
;
1375 event
.m_keyCode
= keyval
;
1379 event
.SetTimestamp(when
);
1380 event
.SetEventObject(focus
);
1381 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;