1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWidgets 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 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar();
344 wxFrame
* win
= mbar
->GetFrame();
347 EventRef formerEvent
= (EventRef
) wxTheApp
->MacGetCurrentEvent() ;
348 EventHandlerCallRef formerEventHandlerCallRef
= (EventHandlerCallRef
) wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
349 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
351 // VZ: we could find the menu from its handle here by examining all
352 // the menus in the menu bar recursively but knowing that neither
353 // wxMSW nor wxGTK do it why bother...
357 GetEventParameter(event
,
358 kEventParamDirectObject
,
360 sizeof(menuRef
), NULL
,
366 switch (GetEventKind(event
))
368 case kEventMenuOpening
:
369 type
= wxEVT_MENU_OPEN
;
371 case kEventMenuClosed
:
372 type
= wxEVT_MENU_CLOSE
;
374 case kEventMenuTargetItem
:
375 type
= wxEVT_MENU_HIGHLIGHT
;
376 GetEventParameter(event
, kEventParamMenuCommand
,
377 typeMenuCommand
, NULL
,
378 sizeof(cmd
), NULL
, &cmd
);
379 if (cmd
== 0) return eventNotHandledErr
;
382 wxFAIL_MSG(wxT("Unexpected menu event kind"));
386 wxMenuEvent
wxevent(type
, cmd
);
387 wxevent
.SetEventObject(win
);
389 (void)win
->GetEventHandler()->ProcessEvent(wxevent
);
391 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerEventHandlerCallRef
) ;
395 return eventNotHandledErr
;
398 static pascal OSStatus
wxMacAppCommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
400 OSStatus result
= eventNotHandledErr
;
404 GetEventParameter( event
, kEventParamDirectObject
, typeHICommand
, NULL
,
405 sizeof( HICommand
), NULL
, &command
);
407 MenuCommand id
= command
.commandID
;
408 if ( id
== kHICommandPreferences
)
409 id
= wxApp::s_macPreferencesMenuItemId
;
411 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
412 wxMenu
* menu
= NULL
;
413 wxMenuItem
* item
= NULL
;
417 item
= mbar
->FindItem( id
, &menu
) ;
418 // it is not 100 % sure that an menu of id 0 is really ours, safety check
419 if ( id
== 0 && menu
!= NULL
&& menu
->GetHMenu() != command
.menu
.menuRef
)
426 if ( item
== NULL
|| menu
== NULL
|| mbar
== NULL
)
429 switch( GetEventKind( event
) )
431 case kEventProcessCommand
:
433 if (item
->IsCheckable())
435 item
->Check( !item
->IsChecked() ) ;
438 menu
->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
442 case kEventCommandUpdateStatus
:
443 // eventually trigger an updateui round
453 static pascal OSStatus
wxMacAppApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
455 OSStatus result
= eventNotHandledErr
;
456 switch ( GetEventKind( event
) )
458 case kEventAppActivated
:
461 wxTheApp
->SetActive( true , NULL
) ;
465 case kEventAppDeactivated
:
468 wxTheApp
->SetActive( false , NULL
) ;
478 pascal OSStatus
wxMacAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
480 OSStatus result
= eventNotHandledErr
;
481 switch( GetEventClass( event
) )
483 case kEventClassCommand
:
484 result
= wxMacAppCommandEventHandler( handler
, event
, data
) ;
486 case kEventClassApplication
:
487 result
= wxMacAppApplicationEventHandler( handler
, event
, data
) ;
489 case kEventClassMenu
:
490 result
= wxMacAppMenuEventHandler( handler
, event
, data
) ;
492 case kEventClassMouse
:
493 result
= wxMacTopLevelMouseEventHandler( handler
, event
, NULL
) ;
495 case kEventClassAppleEvent
:
498 wxMacConvertEventToRecord( event
, &rec
) ;
499 result
= AEProcessAppleEvent( &rec
) ;
509 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler
)
511 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
512 // we know it's there ;-)
513 WXIMPORT
char std::__throws_bad_alloc
;
516 pascal static void wxMacAssertOutputHandler(OSType componentSignature
, UInt32 options
,
517 const char *assertionString
, const char *exceptionLabelString
,
518 const char *errorString
, const char *fileName
, long lineNumber
, void *value
, ConstStr255Param outputMsg
)
520 // flow into assert handling
521 wxString fileNameStr
;
522 wxString assertionStr
;
523 wxString exceptionStr
;
526 fileNameStr
= wxString(fileName
, wxConvLocal
);
527 assertionStr
= wxString(assertionString
, wxConvLocal
);
528 exceptionStr
= wxString((exceptionLabelString
!=0) ? exceptionLabelString
: "", wxConvLocal
) ;
529 errorStr
= wxString((errorString
!=0) ? errorString
: "", wxConvLocal
) ;
531 fileNameStr
= fileName
;
532 assertionStr
= assertionString
;
533 exceptionStr
= (exceptionLabelString
!=0) ? exceptionLabelString
: "" ;
534 errorStr
= (errorString
!=0) ? errorString
: "" ;
539 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
540 assertionStr
.c_str() ,
541 exceptionStr
.c_str() ,
543 fileNameStr
.c_str(), lineNumber
,
547 wxOnAssert(fileNameStr
, lineNumber
, assertionStr
,
548 wxString::Format( wxT("%s %s value (%p)") ,exceptionStr
, errorStr
, value
) ) ;
552 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
557 InstallDebugAssertOutputHandler ( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler
) );
559 UMAInitToolbox( 4, sm_isEmbedded
) ;
560 SetEventMask( everyEvent
) ;
561 UMAShowWatchCursor() ;
563 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
564 // open shared library resources from here since we don't have
565 // __wxinitialize in Mach-O shared libraries
566 wxStAppResource::OpenSharedLibraryResource(NULL
);
570 # if __option(profile)
571 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
576 // now avoid exceptions thrown for new (bad_alloc)
577 // FIXME CS for some changes outside wxMac does not compile anymore
579 std::__throws_bad_alloc
= 0 ;
584 s_macCursorRgn
= ::NewRgn() ;
586 // Mac OS X passes a process serial number command line argument when
587 // the application is launched from the Finder. This argument must be
588 // removed from the command line arguments before being handled by the
589 // application (otherwise applications would need to handle it)
592 static const wxChar
*ARG_PSN
= _T("-psn_");
593 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
595 // remove this argument
597 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
601 if ( !wxAppBase::Initialize(argc
, argv
) )
605 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
609 wxMacCreateNotifierTable() ;
611 UMAShowArrowCursor() ;
616 bool wxApp::OnInitGui()
618 if( !wxAppBase::OnInitGui() )
621 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
625 InstallApplicationEventHandler(
626 GetwxMacAppEventHandlerUPP(),
627 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
632 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
633 NewAEEventHandlerUPP(AEHandleODoc
) ,
635 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
636 NewAEEventHandlerUPP(AEHandleOApp
) ,
638 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
639 NewAEEventHandlerUPP(AEHandlePDoc
) ,
641 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
642 NewAEEventHandlerUPP(AEHandleRApp
) ,
644 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
645 NewAEEventHandlerUPP(AEHandleQuit
) ,
652 void wxApp::CleanUp()
654 wxToolTip::RemoveToolTips() ;
656 // One last chance for pending objects to be cleaned up
657 wxTheApp
->DeletePendingObjects();
659 wxMacDestroyNotifierTable() ;
662 # if __option(profile)
663 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
668 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
669 // close shared library resources from here since we don't have
670 // __wxterminate in Mach-O shared libraries
671 wxStAppResource::CloseSharedLibraryResource();
674 UMACleanupToolbox() ;
675 if (s_macCursorRgn
) {
676 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
683 wxAppBase::CleanUp();
686 //----------------------------------------------------------------------
687 // misc initialization stuff
688 //----------------------------------------------------------------------
690 // extern variable for shared library resource id
691 // need to be able to find it with NSLookupAndBindSymbol
692 short gSharedLibraryResource
= kResFileNotOpened
;
694 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
695 CFBundleRef gSharedLibraryBundle
= NULL
;
696 #endif /* WXMAKINGDLL_CORE && __DARWIN__ */
698 wxStAppResource::wxStAppResource()
700 m_currentRefNum
= CurResFile() ;
701 if ( gSharedLibraryResource
!= kResFileNotOpened
)
703 UseResFile( gSharedLibraryResource
) ;
707 wxStAppResource::~wxStAppResource()
709 if ( m_currentRefNum
!= kResFileNotOpened
)
711 UseResFile( m_currentRefNum
) ;
715 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
717 gSharedLibraryResource
= kResFileNotOpened
;
719 #ifdef WXMAKINGDLL_CORE
720 if ( initBlock
!= NULL
) {
721 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
722 FSSpec
*fileSpec
= NULL
;
724 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
725 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
727 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
728 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
731 if (fileSpec
!= NULL
) {
732 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
737 // Open the shared library resource file if it is not yet open
740 const char *theLibPath
;
742 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWidgets"));
743 if (gSharedLibraryBundle
!= NULL
) {
744 // wxWidgets has been bundled into a framework
745 // load the framework resources
747 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
750 // wxWidgets is a simple dynamic shared library
751 // load the resources from the data fork of a separate resource file
755 OSErr theErr
= noErr
;
757 // get the library path
758 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
759 theModule
= NSModuleForSymbol(theSymbol
);
760 theLibPath
= NSLibraryNameForModule(theModule
);
762 // if we call wxLogDebug from here then, as wxTheApp hasn't been
763 // created yet when we're called from wxApp::Initialize(), wxLog
764 // is going to create a default stderr-based log target instead of
765 // the expected normal GUI one -- don't do it, if we really want
766 // to see this message just use fprintf() here
768 wxLogDebug( wxT("wxMac library installation name is '%s'"),
772 // allocate copy to replace .dylib.* extension with .rsrc
773 if (theLibPath
!= NULL
) {
775 theResPath
= wxString(theLibPath
, wxConvLocal
);
777 theResPath
= wxString(theLibPath
);
779 // replace '_core' with '' in case of multi-lib build
780 theResPath
.Replace(wxT("_core"), wxEmptyString
);
781 // replace ".dylib" shared library extension with ".rsrc"
782 theResPath
.Replace(wxT(".dylib"), wxT(".rsrc"));
783 // Find the begining of the filename
784 theName
= theResPath
.AfterLast('/');
787 wxLogDebug( wxT("wxMac resources file name is '%s'"),
788 theResPath
.mb_str() );
791 theErr
= FSPathMakeRef((UInt8
*) theResPath
.mb_str(), &theResRef
, false);
792 if (theErr
!= noErr
) {
793 // try in current directory (using name only)
794 theErr
= FSPathMakeRef((UInt8
*) theName
.mb_str(), &theResRef
, false);
797 // open the resource file
798 if (theErr
== noErr
) {
799 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
800 &gSharedLibraryResource
);
802 if (theErr
!= noErr
) {
804 wxLogDebug( wxT("unable to open wxMac resource file '%s'\n"),
805 theResPath
.mb_str() );
806 #endif // __WXDEBUG__
811 #endif /* __DARWIN__ */
813 #endif /* WXMAKINGDLL_CORE */
816 void wxStAppResource::CloseSharedLibraryResource()
818 #ifdef WXMAKINGDLL_CORE
819 // Close the shared library resource file
820 if (gSharedLibraryResource
!= kResFileNotOpened
) {
822 if (gSharedLibraryBundle
!= NULL
) {
823 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
824 gSharedLibraryResource
);
825 gSharedLibraryBundle
= NULL
;
828 #endif /* __DARWIN__ */
830 CloseResFile(gSharedLibraryResource
);
832 gSharedLibraryResource
= kResFileNotOpened
;
834 #endif /* WXMAKINGDLL_CORE */
837 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
839 // for shared libraries we have to manually get the correct resource
840 // ref num upon initializing and releasing when terminating, therefore
841 // the __wxinitialize and __wxterminate must be used
844 void __sinit(void); /* (generated by linker) */
845 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
846 pascal void __terminate(void);
849 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
851 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
852 return __initialize( theInitBlock
) ;
855 pascal void __wxterminate(void)
857 wxStAppResource::CloseSharedLibraryResource() ;
861 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
863 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
865 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
866 OSStatus err
= noErr
;
869 switch( GetEventClass( event
) )
871 case kEventClassKeyboard
:
874 switch( GetEventKind(event
) )
876 case kEventRawKeyDown
:
877 rec
->what
= keyDown
;
879 case kEventRawKeyRepeat
:
880 rec
->what
= autoKey
;
882 case kEventRawKeyUp
:
885 case kEventRawKeyModifiersChanged
:
886 rec
->what
= nullEvent
;
895 unsigned char charCode
;
897 GetMouse( &rec
->where
) ;
899 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
900 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
901 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
902 rec
->modifiers
= modifiers
;
903 rec
->message
= (keyCode
<< 8 ) + charCode
;
907 case kEventClassTextInput
:
909 switch( GetEventKind( event
) )
911 case kEventTextInputUnicodeForKeyEvent
:
914 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
918 unsigned char charCode
;
920 GetMouse( &rec
->where
) ;
921 rec
->what
= keyDown
;
922 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
923 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
924 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
925 rec
->modifiers
= modifiers
;
926 rec
->message
= (keyCode
<< 8 ) + charCode
;
943 m_printMode
= wxPRINT_WINDOWS
;
945 m_macCurrentEvent
= NULL
;
946 m_macCurrentEventHandlerCallRef
= NULL
;
949 int wxApp::MainLoop()
953 RunApplicationEventLoop() ;
963 void wxApp::ExitMainLoop()
967 QuitApplicationEventLoop() ;
971 // Is a message/event pending?
972 bool wxApp::Pending()
974 // without the receive event (with pull param = false ) nothing is ever reported
976 ReceiveNextEvent (0, NULL
, kEventDurationNoWait
, false, &theEvent
);
977 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
980 // Dispatch a message.
981 bool wxApp::Dispatch()
988 void wxApp::OnIdle(wxIdleEvent
& event
)
990 wxAppBase::OnIdle(event
);
992 // If they are pending events, we must process them: pending events are
993 // either events to the threads other than main or events posted with
994 // wxPostEvent() functions
995 wxMacProcessNotifierAndPendingEvents();
997 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
998 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1001 void wxApp::WakeUpIdle()
1012 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1015 GetTopWindow()->Close(TRUE
);
1018 // Default behaviour: close the application with prompts. The
1019 // user can veto the close, and therefore the end session.
1020 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1024 if (!GetTopWindow()->Close(!event
.CanVeto()))
1029 extern "C" void wxCYield() ;
1035 // Yield to other processes
1037 bool wxApp::Yield(bool onlyIfNeeded
)
1041 if ( !onlyIfNeeded
)
1043 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1051 // by definition yield should handle all non-processed events
1055 OSStatus status
= noErr
;
1058 s_inReceiveEvent
= true ;
1059 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1060 s_inReceiveEvent
= false ;
1062 if ( status
== eventLoopTimedOutErr
)
1064 // make sure next time the event loop will trigger idle events
1065 sleepTime
= kEventDurationNoWait
;
1067 else if ( status
== eventLoopQuitErr
)
1069 // according to QA1061 this may also occur when a WakeUp Process
1074 MacHandleOneEvent( theEvent
) ;
1075 ReleaseEvent(theEvent
);
1077 } while( status
== noErr
) ;
1079 wxMacProcessNotifierAndPendingEvents() ;
1085 void wxApp::MacDoOneEvent()
1089 s_inReceiveEvent
= true ;
1090 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1091 s_inReceiveEvent
= false ;
1092 if ( status
== eventLoopTimedOutErr
)
1094 if ( wxTheApp
->ProcessIdle() )
1095 sleepTime
= kEventDurationNoWait
;
1097 sleepTime
= kEventDurationSecond
;
1099 else if ( status
== eventLoopQuitErr
)
1101 // according to QA1061 this may also occur when a WakeUp Process
1106 MacHandleOneEvent( theEvent
) ;
1107 ReleaseEvent(theEvent
);
1108 sleepTime
= kEventDurationNoWait
;
1112 DeletePendingObjects() ;
1113 wxMacProcessNotifierAndPendingEvents() ;
1116 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1118 // Override to process unhandled events as you please
1121 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1123 EventTargetRef theTarget
;
1124 theTarget
= GetEventDispatcherTarget();
1125 m_macCurrentEvent
= evr
;
1126 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1127 if(status
== eventNotHandledErr
)
1129 MacHandleUnhandledEvent(evr
);
1131 wxMacProcessNotifierAndPendingEvents() ;
1133 wxMutexGuiLeaveOrEnter();
1134 #endif // wxUSE_THREADS
1137 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1138 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1143 case kHomeCharCode
:
1146 case kEnterCharCode
:
1147 retval
= WXK_RETURN
;
1152 case kHelpCharCode
:
1155 case kBackspaceCharCode
:
1161 case kPageUpCharCode
:
1162 retval
= WXK_PAGEUP
;
1164 case kPageDownCharCode
:
1165 retval
= WXK_PAGEDOWN
;
1167 case kReturnCharCode
:
1168 retval
= WXK_RETURN
;
1170 case kFunctionKeyCharCode
:
1222 case kEscapeCharCode
:
1223 retval
= WXK_ESCAPE
;
1225 case kLeftArrowCharCode
:
1228 case kRightArrowCharCode
:
1229 retval
= WXK_RIGHT
;
1231 case kUpArrowCharCode
:
1234 case kDownArrowCharCode
:
1237 case kDeleteCharCode
:
1238 retval
= WXK_DELETE
;
1246 int wxMacKeyCodeToModifier(wxKeyCode key
)
1271 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1274 // wxHIDKeyboard keyboard;
1275 // return keyboard.IsActive(key);
1277 // TODO: Have it use HID Manager on OSX...
1278 //if OS X > 10.2 (i.e. 10.2.x)
1279 //a known apple bug prevents the system from determining led
1280 //states with GetKeys... can only determine caps lock led
1281 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key
));
1283 // KeyMapByteArray keymap;
1284 // GetKeys((BigEndianLong*)keymap);
1285 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1290 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1297 keychar
= short(keymessage
& charCodeMask
);
1298 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1300 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1302 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1303 // and look at the character after
1305 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1306 keychar
= short(keyInfo
& charCodeMask
);
1307 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1309 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1310 long realkeyval
= keyval
;
1311 if ( keyval
== keychar
)
1313 // 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)
1314 realkeyval
= short(keymessage
& charCodeMask
) ;
1315 keyval
= wxToupper( keyval
) ;
1318 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1319 bool handled
= false ;
1320 event
.m_shiftDown
= modifiers
& shiftKey
;
1321 event
.m_controlDown
= modifiers
& controlKey
;
1322 event
.m_altDown
= modifiers
& optionKey
;
1323 event
.m_metaDown
= modifiers
& cmdKey
;
1324 event
.m_keyCode
= keyval
;
1328 event
.m_timeStamp
= when
;
1329 event
.SetEventObject(focus
);
1330 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1331 if ( handled
&& event
.GetSkipped() )
1338 wxWindow
*ancestor
= focus
;
1341 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1344 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1345 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1348 if (ancestor
->IsTopLevel())
1350 ancestor
= ancestor
->GetParent();
1353 #endif // wxUSE_ACCEL
1357 event
.Skip( FALSE
) ;
1358 event
.SetEventType( wxEVT_CHAR
) ;
1360 event
.m_keyCode
= realkeyval
;
1362 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1363 if ( handled
&& event
.GetSkipped() )
1366 if ( !handled
&& (keyval
== WXK_TAB
) )
1368 wxWindow
* iter
= focus
->GetParent() ;
1369 while( iter
&& !handled
)
1371 if ( iter
->HasFlag( wxTAB_TRAVERSAL
) )
1373 wxNavigationKeyEvent new_event
;
1374 new_event
.SetEventObject( focus
);
1375 new_event
.SetDirection( !event
.ShiftDown() );
1376 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1377 new_event
.SetWindowChange( event
.ControlDown() );
1378 new_event
.SetCurrentFocus( focus
);
1379 handled
= focus
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
1380 if ( handled
&& new_event
.GetSkipped() )
1383 iter
= iter
->GetParent() ;
1386 // backdoor handler for default return and command escape
1387 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1389 // if window is not having a focus still testing for default enter or cancel
1390 // TODO add the UMA version for ActiveNonFloatingWindow
1391 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1394 if ( keyval
== WXK_RETURN
)
1396 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1398 if ( def
&& def
->IsEnabled() )
1400 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1401 event
.SetEventObject(def
);
1402 def
->Command(event
);
1406 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1407 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1409 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1410 new_event
.SetEventObject( focus
);
1411 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1418 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1425 keychar
= short(keymessage
& charCodeMask
);
1426 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1427 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1429 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1430 // and look at the character after
1432 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1433 keychar
= short(keyInfo
& charCodeMask
);
1434 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1436 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1438 if ( keyval
== keychar
)
1440 keyval
= wxToupper( keyval
) ;
1442 bool handled
= false ;
1444 wxKeyEvent
event(wxEVT_KEY_UP
);
1445 event
.m_shiftDown
= modifiers
& shiftKey
;
1446 event
.m_controlDown
= modifiers
& controlKey
;
1447 event
.m_altDown
= modifiers
& optionKey
;
1448 event
.m_metaDown
= modifiers
& cmdKey
;
1449 event
.m_keyCode
= keyval
;
1453 event
.m_timeStamp
= when
;
1454 event
.SetEventObject(focus
);
1455 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;