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
310 //----------------------------------------------------------------------
311 // Carbon Event Handler
312 //----------------------------------------------------------------------
314 static const EventTypeSpec eventList
[] =
316 { kEventClassCommand
, kEventProcessCommand
} ,
317 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
319 { kEventClassMenu
, kEventMenuOpening
},
320 { kEventClassMenu
, kEventMenuClosed
},
321 { kEventClassMenu
, kEventMenuTargetItem
},
323 { kEventClassApplication
, kEventAppActivated
} ,
324 { kEventClassApplication
, kEventAppDeactivated
} ,
325 // handling the quit event is not recommended by apple
326 // rather using the quit apple event - which we do
328 { kEventClassAppleEvent
, kEventAppleEvent
} ,
330 { kEventClassMouse
, kEventMouseDown
} ,
331 { kEventClassMouse
, kEventMouseMoved
} ,
332 { kEventClassMouse
, kEventMouseUp
} ,
333 { kEventClassMouse
, kEventMouseDragged
} ,
337 static pascal OSStatus
338 wxMacAppMenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
340 EventRef formerEvent
= (EventRef
) wxTheApp
->MacGetCurrentEvent() ;
341 EventHandlerCallRef formerEventHandlerCallRef
= (EventHandlerCallRef
) wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
342 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
344 wxMacCarbonEvent
cEvent( event
) ;
345 MenuRef menuRef
= cEvent
.GetParameter
<MenuRef
>(kEventParamDirectObject
) ;
346 wxMenu
* menu
= wxFindMenuFromMacMenu( menuRef
) ;
352 switch (GetEventKind(event
))
354 case kEventMenuOpening
:
355 type
= wxEVT_MENU_OPEN
;
357 case kEventMenuClosed
:
358 type
= wxEVT_MENU_CLOSE
;
360 case kEventMenuTargetItem
:
361 cmd
= cEvent
.GetParameter
<MenuCommand
>(kEventParamMenuCommand
,typeMenuCommand
) ;
363 type
= wxEVT_MENU_HIGHLIGHT
;
366 wxFAIL_MSG(wxT("Unexpected menu event kind"));
372 wxMenuEvent
wxevent(type
, cmd
);
373 wxevent
.SetEventObject(menu
);
375 wxEvtHandler
* handler
= menu
->GetEventHandler();
376 if (handler
&& handler
->ProcessEvent(wxevent
))
382 wxWindow
*win
= menu
->GetInvokingWindow();
384 win
->GetEventHandler()->ProcessEvent(wxevent
);
389 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerEventHandlerCallRef
) ;
391 return eventNotHandledErr
;
394 static pascal OSStatus
wxMacAppCommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
396 OSStatus result
= eventNotHandledErr
;
400 wxMacCarbonEvent
cEvent( event
) ;
401 cEvent
.GetParameter
<HICommand
>(kEventParamDirectObject
,typeHICommand
,&command
) ;
403 wxMenuItem
* item
= NULL
;
404 MenuCommand id
= command
.commandID
;
405 // for items we don't really control
406 if ( id
== kHICommandPreferences
)
408 id
= wxApp::s_macPreferencesMenuItemId
;
410 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
413 wxMenu
* menu
= NULL
;
414 item
= mbar
->FindItem( id
, &menu
) ;
417 else if ( id
!= 0 && command
.menu
.menuRef
!= 0 && command
.menu
.menuItemIndex
!= 0 )
419 GetMenuItemRefCon( command
.menu
.menuRef
, command
.menu
.menuItemIndex
, (UInt32
*) &item
) ;
424 switch( cEvent
.GetKind() )
426 case kEventProcessCommand
:
428 if (item
->IsCheckable())
430 item
->Check( !item
->IsChecked() ) ;
433 item
->GetMenu()->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
437 case kEventCommandUpdateStatus
:
438 // eventually trigger an updateui round
448 static pascal OSStatus
wxMacAppApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
450 OSStatus result
= eventNotHandledErr
;
451 switch ( GetEventKind( event
) )
453 case kEventAppActivated
:
456 wxTheApp
->SetActive( true , NULL
) ;
460 case kEventAppDeactivated
:
463 wxTheApp
->SetActive( false , NULL
) ;
473 pascal OSStatus
wxMacAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
475 OSStatus result
= eventNotHandledErr
;
476 switch( GetEventClass( event
) )
478 case kEventClassCommand
:
479 result
= wxMacAppCommandEventHandler( handler
, event
, data
) ;
481 case kEventClassApplication
:
482 result
= wxMacAppApplicationEventHandler( handler
, event
, data
) ;
484 case kEventClassMenu
:
485 result
= wxMacAppMenuEventHandler( handler
, event
, data
) ;
487 case kEventClassMouse
:
488 result
= wxMacTopLevelMouseEventHandler( handler
, event
, NULL
) ;
490 case kEventClassAppleEvent
:
493 wxMacConvertEventToRecord( event
, &rec
) ;
494 result
= AEProcessAppleEvent( &rec
) ;
504 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler
)
506 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
507 // we know it's there ;-)
508 WXIMPORT
char std::__throws_bad_alloc
;
511 pascal static void wxMacAssertOutputHandler(OSType componentSignature
, UInt32 options
,
512 const char *assertionString
, const char *exceptionLabelString
,
513 const char *errorString
, const char *fileName
, long lineNumber
, void *value
, ConstStr255Param outputMsg
)
515 // flow into assert handling
516 wxString fileNameStr
;
517 wxString assertionStr
;
518 wxString exceptionStr
;
521 fileNameStr
= wxString(fileName
, wxConvLocal
);
522 assertionStr
= wxString(assertionString
, wxConvLocal
);
523 exceptionStr
= wxString((exceptionLabelString
!=0) ? exceptionLabelString
: "", wxConvLocal
) ;
524 errorStr
= wxString((errorString
!=0) ? errorString
: "", wxConvLocal
) ;
526 fileNameStr
= fileName
;
527 assertionStr
= assertionString
;
528 exceptionStr
= (exceptionLabelString
!=0) ? exceptionLabelString
: "" ;
529 errorStr
= (errorString
!=0) ? errorString
: "" ;
534 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
535 assertionStr
.c_str() ,
536 exceptionStr
.c_str() ,
538 fileNameStr
.c_str(), lineNumber
,
542 wxOnAssert(fileNameStr
, lineNumber
, assertionStr
,
543 wxString::Format( wxT("%s %s value (%p)") ,exceptionStr
, errorStr
, value
) ) ;
547 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
552 InstallDebugAssertOutputHandler ( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler
) );
554 UMAInitToolbox( 4, sm_isEmbedded
) ;
555 SetEventMask( everyEvent
) ;
556 UMAShowWatchCursor() ;
558 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
559 // open shared library resources from here since we don't have
560 // __wxinitialize in Mach-O shared libraries
561 wxStAppResource::OpenSharedLibraryResource(NULL
);
565 # if __option(profile)
566 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
571 // now avoid exceptions thrown for new (bad_alloc)
572 // FIXME CS for some changes outside wxMac does not compile anymore
574 std::__throws_bad_alloc
= 0 ;
579 s_macCursorRgn
= ::NewRgn() ;
581 // Mac OS X passes a process serial number command line argument when
582 // the application is launched from the Finder. This argument must be
583 // removed from the command line arguments before being handled by the
584 // application (otherwise applications would need to handle it)
587 static const wxChar
*ARG_PSN
= _T("-psn_");
588 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
590 // remove this argument
592 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
596 if ( !wxAppBase::Initialize(argc
, argv
) )
600 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
604 wxMacCreateNotifierTable() ;
606 UMAShowArrowCursor() ;
611 bool wxApp::OnInitGui()
613 if( !wxAppBase::OnInitGui() )
616 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
620 InstallApplicationEventHandler(
621 GetwxMacAppEventHandlerUPP(),
622 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
627 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
628 NewAEEventHandlerUPP(AEHandleODoc
) ,
630 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
631 NewAEEventHandlerUPP(AEHandleOApp
) ,
633 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
634 NewAEEventHandlerUPP(AEHandlePDoc
) ,
636 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
637 NewAEEventHandlerUPP(AEHandleRApp
) ,
639 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
640 NewAEEventHandlerUPP(AEHandleQuit
) ,
647 void wxApp::CleanUp()
649 wxToolTip::RemoveToolTips() ;
651 // One last chance for pending objects to be cleaned up
652 wxTheApp
->DeletePendingObjects();
654 wxMacDestroyNotifierTable() ;
657 # if __option(profile)
658 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
663 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
664 // close shared library resources from here since we don't have
665 // __wxterminate in Mach-O shared libraries
666 wxStAppResource::CloseSharedLibraryResource();
669 UMACleanupToolbox() ;
670 if (s_macCursorRgn
) {
671 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
678 wxAppBase::CleanUp();
681 //----------------------------------------------------------------------
682 // misc initialization stuff
683 //----------------------------------------------------------------------
685 // extern variable for shared library resource id
686 // need to be able to find it with NSLookupAndBindSymbol
687 short gSharedLibraryResource
= kResFileNotOpened
;
689 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
690 CFBundleRef gSharedLibraryBundle
= NULL
;
691 #endif /* WXMAKINGDLL_CORE && __DARWIN__ */
693 wxStAppResource::wxStAppResource()
695 m_currentRefNum
= CurResFile() ;
696 if ( gSharedLibraryResource
!= kResFileNotOpened
)
698 UseResFile( gSharedLibraryResource
) ;
702 wxStAppResource::~wxStAppResource()
704 if ( m_currentRefNum
!= kResFileNotOpened
)
706 UseResFile( m_currentRefNum
) ;
710 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
712 gSharedLibraryResource
= kResFileNotOpened
;
714 #ifdef WXMAKINGDLL_CORE
715 if ( initBlock
!= NULL
) {
716 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
717 FSSpec
*fileSpec
= NULL
;
719 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
720 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
722 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
723 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
726 if (fileSpec
!= NULL
) {
727 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
732 // Open the shared library resource file if it is not yet open
735 const char *theLibPath
;
737 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWidgets"));
738 if (gSharedLibraryBundle
!= NULL
) {
739 // wxWidgets has been bundled into a framework
740 // load the framework resources
742 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
745 // wxWidgets is a simple dynamic shared library
746 // load the resources from the data fork of a separate resource file
750 OSErr theErr
= noErr
;
752 // get the library path
753 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
754 theModule
= NSModuleForSymbol(theSymbol
);
755 theLibPath
= NSLibraryNameForModule(theModule
);
757 // if we call wxLogDebug from here then, as wxTheApp hasn't been
758 // created yet when we're called from wxApp::Initialize(), wxLog
759 // is going to create a default stderr-based log target instead of
760 // the expected normal GUI one -- don't do it, if we really want
761 // to see this message just use fprintf() here
763 wxLogDebug( wxT("wxMac library installation name is '%s'"),
767 // allocate copy to replace .dylib.* extension with .rsrc
768 if (theLibPath
!= NULL
) {
770 theResPath
= wxString(theLibPath
, wxConvLocal
);
772 theResPath
= wxString(theLibPath
);
774 // replace '_core' with '' in case of multi-lib build
775 theResPath
.Replace(wxT("_core"), wxEmptyString
);
776 // replace ".dylib" shared library extension with ".rsrc"
777 theResPath
.Replace(wxT(".dylib"), wxT(".rsrc"));
778 // Find the begining of the filename
779 theName
= theResPath
.AfterLast('/');
782 wxLogDebug( wxT("wxMac resources file name is '%s'"),
783 theResPath
.mb_str() );
786 theErr
= FSPathMakeRef((UInt8
*) theResPath
.mb_str(), &theResRef
, false);
787 if (theErr
!= noErr
) {
788 // try in current directory (using name only)
789 theErr
= FSPathMakeRef((UInt8
*) theName
.mb_str(), &theResRef
, false);
792 // open the resource file
793 if (theErr
== noErr
) {
794 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
795 &gSharedLibraryResource
);
797 if (theErr
!= noErr
) {
799 wxLogDebug( wxT("unable to open wxMac resource file '%s'\n"),
800 theResPath
.mb_str() );
801 #endif // __WXDEBUG__
806 #endif /* __DARWIN__ */
808 #endif /* WXMAKINGDLL_CORE */
811 void wxStAppResource::CloseSharedLibraryResource()
813 #ifdef WXMAKINGDLL_CORE
814 // Close the shared library resource file
815 if (gSharedLibraryResource
!= kResFileNotOpened
) {
817 if (gSharedLibraryBundle
!= NULL
) {
818 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
819 gSharedLibraryResource
);
820 gSharedLibraryBundle
= NULL
;
823 #endif /* __DARWIN__ */
825 CloseResFile(gSharedLibraryResource
);
827 gSharedLibraryResource
= kResFileNotOpened
;
829 #endif /* WXMAKINGDLL_CORE */
832 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
834 // for shared libraries we have to manually get the correct resource
835 // ref num upon initializing and releasing when terminating, therefore
836 // the __wxinitialize and __wxterminate must be used
839 void __sinit(void); /* (generated by linker) */
840 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
841 pascal void __terminate(void);
844 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
846 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
847 return __initialize( theInitBlock
) ;
850 pascal void __wxterminate(void)
852 wxStAppResource::CloseSharedLibraryResource() ;
856 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
858 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
860 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
861 OSStatus err
= noErr
;
864 switch( GetEventClass( event
) )
866 case kEventClassKeyboard
:
869 switch( GetEventKind(event
) )
871 case kEventRawKeyDown
:
872 rec
->what
= keyDown
;
874 case kEventRawKeyRepeat
:
875 rec
->what
= autoKey
;
877 case kEventRawKeyUp
:
880 case kEventRawKeyModifiersChanged
:
881 rec
->what
= nullEvent
;
890 unsigned char charCode
;
892 GetMouse( &rec
->where
) ;
894 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
895 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
896 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
897 rec
->modifiers
= modifiers
;
898 rec
->message
= (keyCode
<< 8 ) + charCode
;
902 case kEventClassTextInput
:
904 switch( GetEventKind( event
) )
906 case kEventTextInputUnicodeForKeyEvent
:
909 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
913 unsigned char charCode
;
915 GetMouse( &rec
->where
) ;
916 rec
->what
= keyDown
;
917 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
918 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
919 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
920 rec
->modifiers
= modifiers
;
921 rec
->message
= (keyCode
<< 8 ) + charCode
;
938 m_printMode
= wxPRINT_WINDOWS
;
940 m_macCurrentEvent
= NULL
;
941 m_macCurrentEventHandlerCallRef
= NULL
;
944 int wxApp::MainLoop()
948 RunApplicationEventLoop() ;
958 void wxApp::ExitMainLoop()
962 QuitApplicationEventLoop() ;
966 // Is a message/event pending?
967 bool wxApp::Pending()
969 // without the receive event (with pull param = false ) nothing is ever reported
971 ReceiveNextEvent (0, NULL
, kEventDurationNoWait
, false, &theEvent
);
972 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
975 // Dispatch a message.
976 bool wxApp::Dispatch()
983 void wxApp::OnIdle(wxIdleEvent
& event
)
985 wxAppBase::OnIdle(event
);
987 // If they are pending events, we must process them: pending events are
988 // either events to the threads other than main or events posted with
989 // wxPostEvent() functions
990 wxMacProcessNotifierAndPendingEvents();
992 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
993 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
996 void wxApp::WakeUpIdle()
1007 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1010 GetTopWindow()->Close(TRUE
);
1013 // Default behaviour: close the application with prompts. The
1014 // user can veto the close, and therefore the end session.
1015 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1019 if (!GetTopWindow()->Close(!event
.CanVeto()))
1024 extern "C" void wxCYield() ;
1030 // Yield to other processes
1032 bool wxApp::Yield(bool onlyIfNeeded
)
1036 if ( !onlyIfNeeded
)
1038 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1046 // by definition yield should handle all non-processed events
1050 OSStatus status
= noErr
;
1053 s_inReceiveEvent
= true ;
1054 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1055 s_inReceiveEvent
= false ;
1057 if ( status
== eventLoopTimedOutErr
)
1059 // make sure next time the event loop will trigger idle events
1060 sleepTime
= kEventDurationNoWait
;
1062 else if ( status
== eventLoopQuitErr
)
1064 // according to QA1061 this may also occur when a WakeUp Process
1069 MacHandleOneEvent( theEvent
) ;
1070 ReleaseEvent(theEvent
);
1072 } while( status
== noErr
) ;
1074 wxMacProcessNotifierAndPendingEvents() ;
1080 void wxApp::MacDoOneEvent()
1084 s_inReceiveEvent
= true ;
1085 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1086 s_inReceiveEvent
= false ;
1087 if ( status
== eventLoopTimedOutErr
)
1089 if ( wxTheApp
->ProcessIdle() )
1090 sleepTime
= kEventDurationNoWait
;
1092 sleepTime
= kEventDurationSecond
;
1094 else if ( status
== eventLoopQuitErr
)
1096 // according to QA1061 this may also occur when a WakeUp Process
1101 MacHandleOneEvent( theEvent
) ;
1102 ReleaseEvent(theEvent
);
1103 sleepTime
= kEventDurationNoWait
;
1107 DeletePendingObjects() ;
1108 wxMacProcessNotifierAndPendingEvents() ;
1111 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1113 // Override to process unhandled events as you please
1116 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1118 EventTargetRef theTarget
;
1119 theTarget
= GetEventDispatcherTarget();
1120 m_macCurrentEvent
= evr
;
1121 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1122 if(status
== eventNotHandledErr
)
1124 MacHandleUnhandledEvent(evr
);
1126 wxMacProcessNotifierAndPendingEvents() ;
1128 wxMutexGuiLeaveOrEnter();
1129 #endif // wxUSE_THREADS
1132 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1133 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1138 case kHomeCharCode
:
1141 case kEnterCharCode
:
1142 retval
= WXK_RETURN
;
1147 case kHelpCharCode
:
1150 case kBackspaceCharCode
:
1156 case kPageUpCharCode
:
1157 retval
= WXK_PAGEUP
;
1159 case kPageDownCharCode
:
1160 retval
= WXK_PAGEDOWN
;
1162 case kReturnCharCode
:
1163 retval
= WXK_RETURN
;
1165 case kFunctionKeyCharCode
:
1217 case kEscapeCharCode
:
1218 retval
= WXK_ESCAPE
;
1220 case kLeftArrowCharCode
:
1223 case kRightArrowCharCode
:
1224 retval
= WXK_RIGHT
;
1226 case kUpArrowCharCode
:
1229 case kDownArrowCharCode
:
1232 case kDeleteCharCode
:
1233 retval
= WXK_DELETE
;
1241 int wxMacKeyCodeToModifier(wxKeyCode key
)
1266 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1269 // wxHIDKeyboard keyboard;
1270 // return keyboard.IsActive(key);
1272 // TODO: Have it use HID Manager on OSX...
1273 //if OS X > 10.2 (i.e. 10.2.x)
1274 //a known apple bug prevents the system from determining led
1275 //states with GetKeys... can only determine caps lock led
1276 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key
));
1278 // KeyMapByteArray keymap;
1279 // GetKeys((BigEndianLong*)keymap);
1280 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1285 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1292 keychar
= short(keymessage
& charCodeMask
);
1293 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1295 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1297 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1298 // and look at the character after
1300 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1301 keychar
= short(keyInfo
& charCodeMask
);
1302 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1304 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1305 long realkeyval
= keyval
;
1306 if ( keyval
== keychar
)
1308 // 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)
1309 realkeyval
= short(keymessage
& charCodeMask
) ;
1310 keyval
= wxToupper( keyval
) ;
1313 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1314 bool handled
= false ;
1315 event
.m_shiftDown
= modifiers
& shiftKey
;
1316 event
.m_controlDown
= modifiers
& controlKey
;
1317 event
.m_altDown
= modifiers
& optionKey
;
1318 event
.m_metaDown
= modifiers
& cmdKey
;
1319 event
.m_keyCode
= keyval
;
1323 event
.m_timeStamp
= when
;
1324 event
.SetEventObject(focus
);
1325 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1326 if ( handled
&& event
.GetSkipped() )
1333 wxWindow
*ancestor
= focus
;
1336 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1339 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1340 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1343 if (ancestor
->IsTopLevel())
1345 ancestor
= ancestor
->GetParent();
1348 #endif // wxUSE_ACCEL
1352 event
.Skip( FALSE
) ;
1353 event
.SetEventType( wxEVT_CHAR
) ;
1355 event
.m_keyCode
= realkeyval
;
1357 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1358 if ( handled
&& event
.GetSkipped() )
1361 if ( !handled
&& (keyval
== WXK_TAB
) )
1363 wxWindow
* iter
= focus
->GetParent() ;
1364 while( iter
&& !handled
)
1366 if ( iter
->HasFlag( wxTAB_TRAVERSAL
) )
1368 wxNavigationKeyEvent new_event
;
1369 new_event
.SetEventObject( focus
);
1370 new_event
.SetDirection( !event
.ShiftDown() );
1371 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1372 new_event
.SetWindowChange( event
.ControlDown() );
1373 new_event
.SetCurrentFocus( focus
);
1374 handled
= focus
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
1375 if ( handled
&& new_event
.GetSkipped() )
1378 iter
= iter
->GetParent() ;
1381 // backdoor handler for default return and command escape
1382 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1384 // if window is not having a focus still testing for default enter or cancel
1385 // TODO add the UMA version for ActiveNonFloatingWindow
1386 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1389 if ( keyval
== WXK_RETURN
)
1391 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1393 if ( def
&& def
->IsEnabled() )
1395 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1396 event
.SetEventObject(def
);
1397 def
->Command(event
);
1401 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1402 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1404 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1405 new_event
.SetEventObject( focus
);
1406 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1413 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1420 keychar
= short(keymessage
& charCodeMask
);
1421 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1422 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1424 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1425 // and look at the character after
1427 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1428 keychar
= short(keyInfo
& charCodeMask
);
1429 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1431 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1433 if ( keyval
== keychar
)
1435 keyval
= wxToupper( keyval
) ;
1437 bool handled
= false ;
1439 wxKeyEvent
event(wxEVT_KEY_UP
);
1440 event
.m_shiftDown
= modifiers
& shiftKey
;
1441 event
.m_controlDown
= modifiers
& controlKey
;
1442 event
.m_altDown
= modifiers
& optionKey
;
1443 event
.m_metaDown
= modifiers
& cmdKey
;
1444 event
.m_keyCode
= keyval
;
1448 event
.m_timeStamp
= when
;
1449 event
.SetEventObject(focus
);
1450 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;