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
;
171 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
175 err
= AECountItems(&docList
, &itemsInList
);
179 ProcessSerialNumber PSN
;
180 PSN
.highLongOfPSN
= 0 ;
181 PSN
.lowLongOfPSN
= kCurrentProcess
;
182 SetFrontProcess( &PSN
) ;
184 for (i
= 1; i
<= itemsInList
; i
++) {
185 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
186 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
187 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
193 // AEPDoc Calls MacPrintFile on each of the files passed
195 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF
WXUNUSED(reply
))
199 DescType returnedType
;
205 err
= AEGetParamDesc((AppleEvent
*)event
, keyDirectObject
, typeAEList
,&docList
);
209 err
= AECountItems(&docList
, &itemsInList
);
213 ProcessSerialNumber PSN
;
214 PSN
.highLongOfPSN
= 0 ;
215 PSN
.lowLongOfPSN
= kCurrentProcess
;
216 SetFrontProcess( &PSN
) ;
218 for (i
= 1; i
<= itemsInList
; i
++) {
219 AEGetNthPtr(&docList
, i
, typeFSS
, &keywd
, &returnedType
,
220 (Ptr
) & theSpec
, sizeof(theSpec
), &actualSize
);
221 wxString fName
= wxMacFSSpec2MacFilename(&theSpec
);
227 // AEOApp calls MacNewFile
229 short wxApp::MacHandleAEOApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
235 // AEQuit attempts to quit the application
237 short wxApp::MacHandleAEQuit(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
239 wxWindow
* win
= GetTopWindow() ;
242 wxCommandEvent
exitEvent(wxEVT_COMMAND_MENU_SELECTED
, s_macExitMenuItemId
);
243 if (!win
->ProcessEvent(exitEvent
))
253 // AEROApp calls MacReopenApp
255 short wxApp::MacHandleAERApp(const WXEVENTREF
WXUNUSED(event
) , WXEVENTREF
WXUNUSED(reply
))
262 //----------------------------------------------------------------------
263 // Support Routines linking the Mac...File Calls to the Document Manager
264 //----------------------------------------------------------------------
266 void wxApp::MacOpenFile(const wxString
& fileName
)
268 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
270 dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
273 void wxApp::MacPrintFile(const wxString
& fileName
)
275 wxDocManager
* dm
= wxDocManager::GetDocumentManager() ;
278 wxDocument
*doc
= dm
->CreateDocument(fileName
, wxDOC_SILENT
) ;
281 wxView
* view
= doc
->GetFirstView() ;
284 wxPrintout
*printout
= view
->OnCreatePrintout();
288 printer
.Print(view
->GetFrame(), printout
, TRUE
);
294 doc
->DeleteAllViews();
295 dm
->RemoveDocument(doc
) ;
301 void wxApp::MacNewFile()
305 void wxApp::MacReopenApp()
307 // eventually check for open docs, if none, call MacNewFile
308 wxTopLevelWindowMac
* topLevelWindow
= wxDynamicCast(GetTopWindow(), wxTopLevelWindowMac
);
310 if (topLevelWindow
&& topLevelWindow
->IsIconized())
311 topLevelWindow
->Iconize(false);
314 //----------------------------------------------------------------------
315 // Carbon Event Handler
316 //----------------------------------------------------------------------
318 static const EventTypeSpec eventList
[] =
320 { kEventClassCommand
, kEventProcessCommand
} ,
321 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
323 { kEventClassMenu
, kEventMenuOpening
},
324 { kEventClassMenu
, kEventMenuClosed
},
325 { kEventClassMenu
, kEventMenuTargetItem
},
327 { kEventClassApplication
, kEventAppActivated
} ,
328 { kEventClassApplication
, kEventAppDeactivated
} ,
329 // handling the quit event is not recommended by apple
330 // rather using the quit apple event - which we do
332 { kEventClassAppleEvent
, kEventAppleEvent
} ,
334 { kEventClassMouse
, kEventMouseDown
} ,
335 { kEventClassMouse
, kEventMouseMoved
} ,
336 { kEventClassMouse
, kEventMouseUp
} ,
337 { kEventClassMouse
, kEventMouseDragged
} ,
341 static pascal OSStatus
342 wxMacAppMenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
344 EventRef formerEvent
= (EventRef
) wxTheApp
->MacGetCurrentEvent() ;
345 EventHandlerCallRef formerEventHandlerCallRef
= (EventHandlerCallRef
) wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
346 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
348 wxMacCarbonEvent
cEvent( event
) ;
349 MenuRef menuRef
= cEvent
.GetParameter
<MenuRef
>(kEventParamDirectObject
) ;
350 wxMenu
* menu
= wxFindMenuFromMacMenu( menuRef
) ;
356 switch (GetEventKind(event
))
358 case kEventMenuOpening
:
359 type
= wxEVT_MENU_OPEN
;
361 case kEventMenuClosed
:
362 type
= wxEVT_MENU_CLOSE
;
364 case kEventMenuTargetItem
:
365 cmd
= cEvent
.GetParameter
<MenuCommand
>(kEventParamMenuCommand
,typeMenuCommand
) ;
367 type
= wxEVT_MENU_HIGHLIGHT
;
370 wxFAIL_MSG(wxT("Unexpected menu event kind"));
376 wxMenuEvent
wxevent(type
, cmd
);
377 wxevent
.SetEventObject(menu
);
379 wxEvtHandler
* handler
= menu
->GetEventHandler();
380 if (handler
&& handler
->ProcessEvent(wxevent
))
386 wxWindow
*win
= menu
->GetInvokingWindow();
388 win
->GetEventHandler()->ProcessEvent(wxevent
);
393 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerEventHandlerCallRef
) ;
395 return eventNotHandledErr
;
398 static pascal OSStatus
wxMacAppCommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
400 OSStatus result
= eventNotHandledErr
;
404 wxMacCarbonEvent
cEvent( event
) ;
405 cEvent
.GetParameter
<HICommand
>(kEventParamDirectObject
,typeHICommand
,&command
) ;
407 wxMenuItem
* item
= NULL
;
408 MenuCommand id
= command
.commandID
;
409 // for items we don't really control
410 if ( id
== kHICommandPreferences
)
412 id
= wxApp::s_macPreferencesMenuItemId
;
414 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
417 wxMenu
* menu
= NULL
;
418 item
= mbar
->FindItem( id
, &menu
) ;
421 else if ( id
!= 0 && command
.menu
.menuRef
!= 0 && command
.menu
.menuItemIndex
!= 0 )
423 GetMenuItemRefCon( command
.menu
.menuRef
, command
.menu
.menuItemIndex
, (UInt32
*) &item
) ;
428 switch( cEvent
.GetKind() )
430 case kEventProcessCommand
:
432 if (item
->IsCheckable())
434 item
->Check( !item
->IsChecked() ) ;
437 item
->GetMenu()->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
441 case kEventCommandUpdateStatus
:
442 // eventually trigger an updateui round
452 static pascal OSStatus
wxMacAppApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
454 OSStatus result
= eventNotHandledErr
;
455 switch ( GetEventKind( event
) )
457 case kEventAppActivated
:
460 wxTheApp
->SetActive( true , NULL
) ;
464 case kEventAppDeactivated
:
467 wxTheApp
->SetActive( false , NULL
) ;
477 pascal OSStatus
wxMacAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
479 OSStatus result
= eventNotHandledErr
;
480 switch( GetEventClass( event
) )
482 case kEventClassCommand
:
483 result
= wxMacAppCommandEventHandler( handler
, event
, data
) ;
485 case kEventClassApplication
:
486 result
= wxMacAppApplicationEventHandler( handler
, event
, data
) ;
488 case kEventClassMenu
:
489 result
= wxMacAppMenuEventHandler( handler
, event
, data
) ;
491 case kEventClassMouse
:
492 result
= wxMacTopLevelMouseEventHandler( handler
, event
, NULL
) ;
494 case kEventClassAppleEvent
:
497 wxMacConvertEventToRecord( event
, &rec
) ;
498 result
= AEProcessAppleEvent( &rec
) ;
508 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler
)
510 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
511 // we know it's there ;-)
512 WXIMPORT
char std::__throws_bad_alloc
;
515 pascal static void wxMacAssertOutputHandler(OSType componentSignature
, UInt32 options
,
516 const char *assertionString
, const char *exceptionLabelString
,
517 const char *errorString
, const char *fileName
, long lineNumber
, void *value
, ConstStr255Param outputMsg
)
519 // flow into assert handling
520 wxString fileNameStr
;
521 wxString assertionStr
;
522 wxString exceptionStr
;
525 fileNameStr
= wxString(fileName
, wxConvLocal
);
526 assertionStr
= wxString(assertionString
, wxConvLocal
);
527 exceptionStr
= wxString((exceptionLabelString
!=0) ? exceptionLabelString
: "", wxConvLocal
) ;
528 errorStr
= wxString((errorString
!=0) ? errorString
: "", wxConvLocal
) ;
530 fileNameStr
= fileName
;
531 assertionStr
= assertionString
;
532 exceptionStr
= (exceptionLabelString
!=0) ? exceptionLabelString
: "" ;
533 errorStr
= (errorString
!=0) ? errorString
: "" ;
538 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
539 assertionStr
.c_str() ,
540 exceptionStr
.c_str() ,
542 fileNameStr
.c_str(), lineNumber
,
546 wxOnAssert(fileNameStr
, lineNumber
, assertionStr
,
547 wxString::Format( wxT("%s %s value (%p)") ,exceptionStr
, errorStr
, value
) ) ;
551 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
556 InstallDebugAssertOutputHandler ( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler
) );
558 UMAInitToolbox( 4, sm_isEmbedded
) ;
559 SetEventMask( everyEvent
) ;
560 UMAShowWatchCursor() ;
562 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
563 // open shared library resources from here since we don't have
564 // __wxinitialize in Mach-O shared libraries
565 wxStAppResource::OpenSharedLibraryResource(NULL
);
569 # if __option(profile)
570 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
575 // now avoid exceptions thrown for new (bad_alloc)
576 // FIXME CS for some changes outside wxMac does not compile anymore
578 std::__throws_bad_alloc
= 0 ;
583 s_macCursorRgn
= ::NewRgn() ;
585 // Mac OS X passes a process serial number command line argument when
586 // the application is launched from the Finder. This argument must be
587 // removed from the command line arguments before being handled by the
588 // application (otherwise applications would need to handle it)
591 static const wxChar
*ARG_PSN
= _T("-psn_");
592 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
594 // remove this argument
596 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
600 if ( !wxAppBase::Initialize(argc
, argv
) )
604 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
608 wxMacCreateNotifierTable() ;
610 UMAShowArrowCursor() ;
615 bool wxApp::OnInitGui()
617 if( !wxAppBase::OnInitGui() )
620 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
624 InstallApplicationEventHandler(
625 GetwxMacAppEventHandlerUPP(),
626 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
631 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
632 NewAEEventHandlerUPP(AEHandleODoc
) ,
634 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
635 NewAEEventHandlerUPP(AEHandleOApp
) ,
637 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
638 NewAEEventHandlerUPP(AEHandlePDoc
) ,
640 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
641 NewAEEventHandlerUPP(AEHandleRApp
) ,
643 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
644 NewAEEventHandlerUPP(AEHandleQuit
) ,
651 void wxApp::CleanUp()
653 wxToolTip::RemoveToolTips() ;
655 // One last chance for pending objects to be cleaned up
656 wxTheApp
->DeletePendingObjects();
658 wxMacDestroyNotifierTable() ;
661 # if __option(profile)
662 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
667 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
668 // close shared library resources from here since we don't have
669 // __wxterminate in Mach-O shared libraries
670 wxStAppResource::CloseSharedLibraryResource();
673 UMACleanupToolbox() ;
674 if (s_macCursorRgn
) {
675 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
682 wxAppBase::CleanUp();
685 //----------------------------------------------------------------------
686 // misc initialization stuff
687 //----------------------------------------------------------------------
689 // extern variable for shared library resource id
690 // need to be able to find it with NSLookupAndBindSymbol
691 short gSharedLibraryResource
= kResFileNotOpened
;
693 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
694 CFBundleRef gSharedLibraryBundle
= NULL
;
695 #endif /* WXMAKINGDLL_CORE && __DARWIN__ */
697 wxStAppResource::wxStAppResource()
699 m_currentRefNum
= CurResFile() ;
700 if ( gSharedLibraryResource
!= kResFileNotOpened
)
702 UseResFile( gSharedLibraryResource
) ;
706 wxStAppResource::~wxStAppResource()
708 if ( m_currentRefNum
!= kResFileNotOpened
)
710 UseResFile( m_currentRefNum
) ;
714 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
716 gSharedLibraryResource
= kResFileNotOpened
;
718 #ifdef WXMAKINGDLL_CORE
719 if ( initBlock
!= NULL
) {
720 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
721 FSSpec
*fileSpec
= NULL
;
723 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
724 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
726 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
727 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
730 if (fileSpec
!= NULL
) {
731 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
736 // Open the shared library resource file if it is not yet open
739 const char *theLibPath
;
741 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWidgets"));
742 if (gSharedLibraryBundle
!= NULL
) {
743 // wxWidgets has been bundled into a framework
744 // load the framework resources
746 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
749 // wxWidgets is a simple dynamic shared library
750 // load the resources from the data fork of a separate resource file
754 OSErr theErr
= noErr
;
756 // get the library path
757 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
758 theModule
= NSModuleForSymbol(theSymbol
);
759 theLibPath
= NSLibraryNameForModule(theModule
);
761 // if we call wxLogDebug from here then, as wxTheApp hasn't been
762 // created yet when we're called from wxApp::Initialize(), wxLog
763 // is going to create a default stderr-based log target instead of
764 // the expected normal GUI one -- don't do it, if we really want
765 // to see this message just use fprintf() here
767 wxLogDebug( wxT("wxMac library installation name is '%s'"),
771 // allocate copy to replace .dylib.* extension with .rsrc
772 if (theLibPath
!= NULL
) {
774 theResPath
= wxString(theLibPath
, wxConvLocal
);
776 theResPath
= wxString(theLibPath
);
778 // replace '_core' with '' in case of multi-lib build
779 theResPath
.Replace(wxT("_core"), wxEmptyString
);
780 // replace ".dylib" shared library extension with ".rsrc"
781 theResPath
.Replace(wxT(".dylib"), wxT(".rsrc"));
782 // Find the begining of the filename
783 theName
= theResPath
.AfterLast('/');
786 wxLogDebug( wxT("wxMac resources file name is '%s'"),
787 theResPath
.mb_str() );
790 theErr
= FSPathMakeRef((UInt8
*) theResPath
.mb_str(), &theResRef
, false);
791 if (theErr
!= noErr
) {
792 // try in current directory (using name only)
793 theErr
= FSPathMakeRef((UInt8
*) theName
.mb_str(), &theResRef
, false);
796 // open the resource file
797 if (theErr
== noErr
) {
798 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
799 &gSharedLibraryResource
);
801 if (theErr
!= noErr
) {
803 wxLogDebug( wxT("unable to open wxMac resource file '%s'\n"),
804 theResPath
.mb_str() );
805 #endif // __WXDEBUG__
810 #endif /* __DARWIN__ */
812 #endif /* WXMAKINGDLL_CORE */
815 void wxStAppResource::CloseSharedLibraryResource()
817 #ifdef WXMAKINGDLL_CORE
818 // Close the shared library resource file
819 if (gSharedLibraryResource
!= kResFileNotOpened
) {
821 if (gSharedLibraryBundle
!= NULL
) {
822 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
823 gSharedLibraryResource
);
824 gSharedLibraryBundle
= NULL
;
827 #endif /* __DARWIN__ */
829 CloseResFile(gSharedLibraryResource
);
831 gSharedLibraryResource
= kResFileNotOpened
;
833 #endif /* WXMAKINGDLL_CORE */
836 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
838 // for shared libraries we have to manually get the correct resource
839 // ref num upon initializing and releasing when terminating, therefore
840 // the __wxinitialize and __wxterminate must be used
843 void __sinit(void); /* (generated by linker) */
844 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
845 pascal void __terminate(void);
848 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
850 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
851 return __initialize( theInitBlock
) ;
854 pascal void __wxterminate(void)
856 wxStAppResource::CloseSharedLibraryResource() ;
860 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
862 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
864 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
865 OSStatus err
= noErr
;
868 switch( GetEventClass( event
) )
870 case kEventClassKeyboard
:
873 switch( GetEventKind(event
) )
875 case kEventRawKeyDown
:
876 rec
->what
= keyDown
;
878 case kEventRawKeyRepeat
:
879 rec
->what
= autoKey
;
881 case kEventRawKeyUp
:
884 case kEventRawKeyModifiersChanged
:
885 rec
->what
= nullEvent
;
894 unsigned char charCode
;
896 GetMouse( &rec
->where
) ;
898 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
899 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
900 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
901 rec
->modifiers
= modifiers
;
902 rec
->message
= (keyCode
<< 8 ) + charCode
;
906 case kEventClassTextInput
:
908 switch( GetEventKind( event
) )
910 case kEventTextInputUnicodeForKeyEvent
:
913 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
917 unsigned char charCode
;
919 GetMouse( &rec
->where
) ;
920 rec
->what
= keyDown
;
921 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
922 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
923 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
924 rec
->modifiers
= modifiers
;
925 rec
->message
= (keyCode
<< 8 ) + charCode
;
942 m_printMode
= wxPRINT_WINDOWS
;
944 m_macCurrentEvent
= NULL
;
945 m_macCurrentEventHandlerCallRef
= NULL
;
948 int wxApp::MainLoop()
952 RunApplicationEventLoop() ;
962 void wxApp::ExitMainLoop()
966 QuitApplicationEventLoop() ;
970 // Is a message/event pending?
971 bool wxApp::Pending()
973 // without the receive event (with pull param = false ) nothing is ever reported
975 ReceiveNextEvent (0, NULL
, kEventDurationNoWait
, false, &theEvent
);
976 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
979 // Dispatch a message.
980 bool wxApp::Dispatch()
987 void wxApp::OnIdle(wxIdleEvent
& event
)
989 wxAppBase::OnIdle(event
);
991 // If they are pending events, we must process them: pending events are
992 // either events to the threads other than main or events posted with
993 // wxPostEvent() functions
994 wxMacProcessNotifierAndPendingEvents();
996 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
997 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1000 void wxApp::WakeUpIdle()
1011 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1014 GetTopWindow()->Close(TRUE
);
1017 // Default behaviour: close the application with prompts. The
1018 // user can veto the close, and therefore the end session.
1019 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1023 if (!GetTopWindow()->Close(!event
.CanVeto()))
1028 extern "C" void wxCYield() ;
1034 // Yield to other processes
1036 bool wxApp::Yield(bool onlyIfNeeded
)
1040 if ( !onlyIfNeeded
)
1042 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1050 // by definition yield should handle all non-processed events
1054 OSStatus status
= noErr
;
1057 s_inReceiveEvent
= true ;
1058 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1059 s_inReceiveEvent
= false ;
1061 if ( status
== eventLoopTimedOutErr
)
1063 // make sure next time the event loop will trigger idle events
1064 sleepTime
= kEventDurationNoWait
;
1066 else if ( status
== eventLoopQuitErr
)
1068 // according to QA1061 this may also occur when a WakeUp Process
1073 MacHandleOneEvent( theEvent
) ;
1074 ReleaseEvent(theEvent
);
1076 } while( status
== noErr
) ;
1078 wxMacProcessNotifierAndPendingEvents() ;
1084 void wxApp::MacDoOneEvent()
1088 s_inReceiveEvent
= true ;
1089 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1090 s_inReceiveEvent
= false ;
1091 if ( status
== eventLoopTimedOutErr
)
1093 if ( wxTheApp
->ProcessIdle() )
1094 sleepTime
= kEventDurationNoWait
;
1096 sleepTime
= kEventDurationSecond
;
1098 else if ( status
== eventLoopQuitErr
)
1100 // according to QA1061 this may also occur when a WakeUp Process
1105 MacHandleOneEvent( theEvent
) ;
1106 ReleaseEvent(theEvent
);
1107 sleepTime
= kEventDurationNoWait
;
1111 DeletePendingObjects() ;
1112 wxMacProcessNotifierAndPendingEvents() ;
1115 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1117 // Override to process unhandled events as you please
1120 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1122 EventTargetRef theTarget
;
1123 theTarget
= GetEventDispatcherTarget();
1124 m_macCurrentEvent
= evr
;
1125 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1126 if(status
== eventNotHandledErr
)
1128 MacHandleUnhandledEvent(evr
);
1130 wxMacProcessNotifierAndPendingEvents() ;
1132 wxMutexGuiLeaveOrEnter();
1133 #endif // wxUSE_THREADS
1136 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1137 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1142 case kHomeCharCode
:
1145 case kEnterCharCode
:
1146 retval
= WXK_RETURN
;
1151 case kHelpCharCode
:
1154 case kBackspaceCharCode
:
1160 case kPageUpCharCode
:
1161 retval
= WXK_PAGEUP
;
1163 case kPageDownCharCode
:
1164 retval
= WXK_PAGEDOWN
;
1166 case kReturnCharCode
:
1167 retval
= WXK_RETURN
;
1169 case kFunctionKeyCharCode
:
1221 case kEscapeCharCode
:
1222 retval
= WXK_ESCAPE
;
1224 case kLeftArrowCharCode
:
1227 case kRightArrowCharCode
:
1228 retval
= WXK_RIGHT
;
1230 case kUpArrowCharCode
:
1233 case kDownArrowCharCode
:
1236 case kDeleteCharCode
:
1237 retval
= WXK_DELETE
;
1245 int wxMacKeyCodeToModifier(wxKeyCode key
)
1270 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1273 // wxHIDKeyboard keyboard;
1274 // return keyboard.IsActive(key);
1276 // TODO: Have it use HID Manager on OSX...
1277 //if OS X > 10.2 (i.e. 10.2.x)
1278 //a known apple bug prevents the system from determining led
1279 //states with GetKeys... can only determine caps lock led
1280 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key
));
1282 // KeyMapByteArray keymap;
1283 // GetKeys((BigEndianLong*)keymap);
1284 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1289 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1296 keychar
= short(keymessage
& charCodeMask
);
1297 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1299 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1301 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1302 // and look at the character after
1304 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1305 keychar
= short(keyInfo
& charCodeMask
);
1306 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1308 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1309 long realkeyval
= keyval
;
1310 if ( keyval
== keychar
)
1312 // 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)
1313 realkeyval
= short(keymessage
& charCodeMask
) ;
1314 keyval
= wxToupper( keyval
) ;
1317 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1318 bool handled
= false ;
1319 event
.m_shiftDown
= modifiers
& shiftKey
;
1320 event
.m_controlDown
= modifiers
& controlKey
;
1321 event
.m_altDown
= modifiers
& optionKey
;
1322 event
.m_metaDown
= modifiers
& cmdKey
;
1323 event
.m_keyCode
= keyval
;
1327 event
.m_timeStamp
= when
;
1328 event
.SetEventObject(focus
);
1329 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1330 if ( handled
&& event
.GetSkipped() )
1337 wxWindow
*ancestor
= focus
;
1340 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1343 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1344 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1347 if (ancestor
->IsTopLevel())
1349 ancestor
= ancestor
->GetParent();
1352 #endif // wxUSE_ACCEL
1356 event
.Skip( FALSE
) ;
1357 event
.SetEventType( wxEVT_CHAR
) ;
1359 event
.m_keyCode
= realkeyval
;
1361 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1362 if ( handled
&& event
.GetSkipped() )
1365 if ( !handled
&& (keyval
== WXK_TAB
) )
1367 wxWindow
* iter
= focus
->GetParent() ;
1368 while( iter
&& !handled
)
1370 if ( iter
->HasFlag( wxTAB_TRAVERSAL
) )
1372 wxNavigationKeyEvent new_event
;
1373 new_event
.SetEventObject( focus
);
1374 new_event
.SetDirection( !event
.ShiftDown() );
1375 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1376 new_event
.SetWindowChange( event
.ControlDown() );
1377 new_event
.SetCurrentFocus( focus
);
1378 handled
= focus
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
1379 if ( handled
&& new_event
.GetSkipped() )
1382 iter
= iter
->GetParent() ;
1385 // backdoor handler for default return and command escape
1386 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1388 // if window is not having a focus still testing for default enter or cancel
1389 // TODO add the UMA version for ActiveNonFloatingWindow
1390 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1393 if ( keyval
== WXK_RETURN
)
1395 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1397 if ( def
&& def
->IsEnabled() )
1399 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1400 event
.SetEventObject(def
);
1401 def
->Command(event
);
1405 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1406 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1408 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1409 new_event
.SetEventObject( focus
);
1410 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1417 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1424 keychar
= short(keymessage
& charCodeMask
);
1425 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1426 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1428 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1429 // and look at the character after
1431 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1432 keychar
= short(keyInfo
& charCodeMask
);
1433 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1435 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1437 if ( keyval
== keychar
)
1439 keyval
= wxToupper( keyval
) ;
1441 bool handled
= false ;
1443 wxKeyEvent
event(wxEVT_KEY_UP
);
1444 event
.m_shiftDown
= modifiers
& shiftKey
;
1445 event
.m_controlDown
= modifiers
& controlKey
;
1446 event
.m_altDown
= modifiers
& optionKey
;
1447 event
.m_metaDown
= modifiers
& cmdKey
;
1448 event
.m_keyCode
= keyval
;
1452 event
.m_timeStamp
= when
;
1453 event
.SetEventObject(focus
);
1454 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;