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()
308 // if there is no open window -> create a new one
309 // if all windows are hidden -> show the first
310 // if some windows are not hidden -> do nothing
312 wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
319 wxTopLevelWindow
* win
= NULL
;
320 wxTopLevelWindow
* firstIconized
= NULL
;
323 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->GetData();
324 if ( win
->IsIconized() == false )
326 firstIconized
= NULL
;
331 if ( firstIconized
== NULL
)
332 firstIconized
= win
;
334 node
= node
->GetNext();
337 firstIconized
->Iconize( false ) ;
341 //----------------------------------------------------------------------
342 // Carbon Event Handler
343 //----------------------------------------------------------------------
345 static const EventTypeSpec eventList
[] =
347 { kEventClassCommand
, kEventProcessCommand
} ,
348 { kEventClassCommand
, kEventCommandUpdateStatus
} ,
350 { kEventClassMenu
, kEventMenuOpening
},
351 { kEventClassMenu
, kEventMenuClosed
},
352 { kEventClassMenu
, kEventMenuTargetItem
},
354 { kEventClassApplication
, kEventAppActivated
} ,
355 { kEventClassApplication
, kEventAppDeactivated
} ,
356 // handling the quit event is not recommended by apple
357 // rather using the quit apple event - which we do
359 { kEventClassAppleEvent
, kEventAppleEvent
} ,
361 { kEventClassMouse
, kEventMouseDown
} ,
362 { kEventClassMouse
, kEventMouseMoved
} ,
363 { kEventClassMouse
, kEventMouseUp
} ,
364 { kEventClassMouse
, kEventMouseDragged
} ,
368 static pascal OSStatus
369 wxMacAppMenuEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
371 EventRef formerEvent
= (EventRef
) wxTheApp
->MacGetCurrentEvent() ;
372 EventHandlerCallRef formerEventHandlerCallRef
= (EventHandlerCallRef
) wxTheApp
->MacGetCurrentEventHandlerCallRef() ;
373 wxTheApp
->MacSetCurrentEvent( event
, handler
) ;
375 wxMacCarbonEvent
cEvent( event
) ;
376 MenuRef menuRef
= cEvent
.GetParameter
<MenuRef
>(kEventParamDirectObject
) ;
377 wxMenu
* menu
= wxFindMenuFromMacMenu( menuRef
) ;
383 switch (GetEventKind(event
))
385 case kEventMenuOpening
:
386 type
= wxEVT_MENU_OPEN
;
388 case kEventMenuClosed
:
389 type
= wxEVT_MENU_CLOSE
;
391 case kEventMenuTargetItem
:
392 cmd
= cEvent
.GetParameter
<MenuCommand
>(kEventParamMenuCommand
,typeMenuCommand
) ;
394 type
= wxEVT_MENU_HIGHLIGHT
;
397 wxFAIL_MSG(wxT("Unexpected menu event kind"));
403 wxMenuEvent
wxevent(type
, cmd
);
404 wxevent
.SetEventObject(menu
);
406 wxEvtHandler
* handler
= menu
->GetEventHandler();
407 if (handler
&& handler
->ProcessEvent(wxevent
))
413 wxWindow
*win
= menu
->GetInvokingWindow();
415 win
->GetEventHandler()->ProcessEvent(wxevent
);
420 wxTheApp
->MacSetCurrentEvent( formerEvent
, formerEventHandlerCallRef
) ;
422 return eventNotHandledErr
;
425 static pascal OSStatus
wxMacAppCommandEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
427 OSStatus result
= eventNotHandledErr
;
431 wxMacCarbonEvent
cEvent( event
) ;
432 cEvent
.GetParameter
<HICommand
>(kEventParamDirectObject
,typeHICommand
,&command
) ;
434 wxMenuItem
* item
= NULL
;
435 MenuCommand id
= command
.commandID
;
436 // for items we don't really control
437 if ( id
== kHICommandPreferences
)
439 id
= wxApp::s_macPreferencesMenuItemId
;
441 wxMenuBar
* mbar
= wxMenuBar::MacGetInstalledMenuBar() ;
444 wxMenu
* menu
= NULL
;
445 item
= mbar
->FindItem( id
, &menu
) ;
448 else if ( id
!= 0 && command
.menu
.menuRef
!= 0 && command
.menu
.menuItemIndex
!= 0 )
450 GetMenuItemRefCon( command
.menu
.menuRef
, command
.menu
.menuItemIndex
, (UInt32
*) &item
) ;
455 switch( cEvent
.GetKind() )
457 case kEventProcessCommand
:
459 if (item
->IsCheckable())
461 item
->Check( !item
->IsChecked() ) ;
464 item
->GetMenu()->SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) ;
468 case kEventCommandUpdateStatus
:
469 // eventually trigger an updateui round
479 static pascal OSStatus
wxMacAppApplicationEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
481 OSStatus result
= eventNotHandledErr
;
482 switch ( GetEventKind( event
) )
484 case kEventAppActivated
:
487 wxTheApp
->SetActive( true , NULL
) ;
491 case kEventAppDeactivated
:
494 wxTheApp
->SetActive( false , NULL
) ;
504 pascal OSStatus
wxMacAppEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
506 OSStatus result
= eventNotHandledErr
;
507 switch( GetEventClass( event
) )
509 case kEventClassCommand
:
510 result
= wxMacAppCommandEventHandler( handler
, event
, data
) ;
512 case kEventClassApplication
:
513 result
= wxMacAppApplicationEventHandler( handler
, event
, data
) ;
515 case kEventClassMenu
:
516 result
= wxMacAppMenuEventHandler( handler
, event
, data
) ;
518 case kEventClassMouse
:
519 result
= wxMacTopLevelMouseEventHandler( handler
, event
, NULL
) ;
521 case kEventClassAppleEvent
:
524 wxMacConvertEventToRecord( event
, &rec
) ;
525 result
= AEProcessAppleEvent( &rec
) ;
535 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler
)
537 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
538 // we know it's there ;-)
539 WXIMPORT
char std::__throws_bad_alloc
;
542 pascal static void wxMacAssertOutputHandler(OSType componentSignature
, UInt32 options
,
543 const char *assertionString
, const char *exceptionLabelString
,
544 const char *errorString
, const char *fileName
, long lineNumber
, void *value
, ConstStr255Param outputMsg
)
546 // flow into assert handling
547 wxString fileNameStr
;
548 wxString assertionStr
;
549 wxString exceptionStr
;
552 fileNameStr
= wxString(fileName
, wxConvLocal
);
553 assertionStr
= wxString(assertionString
, wxConvLocal
);
554 exceptionStr
= wxString((exceptionLabelString
!=0) ? exceptionLabelString
: "", wxConvLocal
) ;
555 errorStr
= wxString((errorString
!=0) ? errorString
: "", wxConvLocal
) ;
557 fileNameStr
= fileName
;
558 assertionStr
= assertionString
;
559 exceptionStr
= (exceptionLabelString
!=0) ? exceptionLabelString
: "" ;
560 errorStr
= (errorString
!=0) ? errorString
: "" ;
565 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
566 assertionStr
.c_str() ,
567 exceptionStr
.c_str() ,
569 fileNameStr
.c_str(), lineNumber
,
573 wxOnAssert(fileNameStr
, lineNumber
, assertionStr
,
574 wxString::Format( wxT("%s %s value (%p)") ,exceptionStr
, errorStr
, value
) ) ;
578 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
583 InstallDebugAssertOutputHandler ( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler
) );
585 UMAInitToolbox( 4, sm_isEmbedded
) ;
586 SetEventMask( everyEvent
) ;
587 UMAShowWatchCursor() ;
589 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
590 // open shared library resources from here since we don't have
591 // __wxinitialize in Mach-O shared libraries
592 wxStAppResource::OpenSharedLibraryResource(NULL
);
596 # if __option(profile)
597 ProfilerInit( collectDetailed
, bestTimeBase
, 40000 , 50 ) ;
602 // now avoid exceptions thrown for new (bad_alloc)
603 // FIXME CS for some changes outside wxMac does not compile anymore
605 std::__throws_bad_alloc
= 0 ;
610 s_macCursorRgn
= ::NewRgn() ;
612 // Mac OS X passes a process serial number command line argument when
613 // the application is launched from the Finder. This argument must be
614 // removed from the command line arguments before being handled by the
615 // application (otherwise applications would need to handle it)
618 static const wxChar
*ARG_PSN
= _T("-psn_");
619 if ( wxStrncmp(argv
[1], ARG_PSN
, wxStrlen(ARG_PSN
)) == 0 )
621 // remove this argument
623 memmove(argv
+ 1, argv
+ 2, argc
* sizeof(char *));
627 if ( !wxAppBase::Initialize(argc
, argv
) )
631 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
635 wxMacCreateNotifierTable() ;
637 UMAShowArrowCursor() ;
642 bool wxApp::OnInitGui()
644 if( !wxAppBase::OnInitGui() )
647 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
651 InstallApplicationEventHandler(
652 GetwxMacAppEventHandlerUPP(),
653 GetEventTypeCount(eventList
), eventList
, wxTheApp
, (EventHandlerRef
*)&(wxTheApp
->m_macEventHandler
));
658 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
,
659 NewAEEventHandlerUPP(AEHandleODoc
) ,
661 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
,
662 NewAEEventHandlerUPP(AEHandleOApp
) ,
664 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
,
665 NewAEEventHandlerUPP(AEHandlePDoc
) ,
667 AEInstallEventHandler( kCoreEventClass
, kAEReopenApplication
,
668 NewAEEventHandlerUPP(AEHandleRApp
) ,
670 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
,
671 NewAEEventHandlerUPP(AEHandleQuit
) ,
678 void wxApp::CleanUp()
680 wxToolTip::RemoveToolTips() ;
682 // One last chance for pending objects to be cleaned up
683 wxTheApp
->DeletePendingObjects();
685 wxMacDestroyNotifierTable() ;
688 # if __option(profile)
689 ProfilerDump( (StringPtr
)"\papp.prof" ) ;
694 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
695 // close shared library resources from here since we don't have
696 // __wxterminate in Mach-O shared libraries
697 wxStAppResource::CloseSharedLibraryResource();
700 UMACleanupToolbox() ;
701 if (s_macCursorRgn
) {
702 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
709 wxAppBase::CleanUp();
712 //----------------------------------------------------------------------
713 // misc initialization stuff
714 //----------------------------------------------------------------------
716 // extern variable for shared library resource id
717 // need to be able to find it with NSLookupAndBindSymbol
718 short gSharedLibraryResource
= kResFileNotOpened
;
720 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
721 CFBundleRef gSharedLibraryBundle
= NULL
;
722 #endif /* WXMAKINGDLL_CORE && __DARWIN__ */
724 wxStAppResource::wxStAppResource()
726 m_currentRefNum
= CurResFile() ;
727 if ( gSharedLibraryResource
!= kResFileNotOpened
)
729 UseResFile( gSharedLibraryResource
) ;
733 wxStAppResource::~wxStAppResource()
735 if ( m_currentRefNum
!= kResFileNotOpened
)
737 UseResFile( m_currentRefNum
) ;
741 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock
)
743 gSharedLibraryResource
= kResFileNotOpened
;
745 #ifdef WXMAKINGDLL_CORE
746 if ( initBlock
!= NULL
) {
747 const CFragInitBlock
*theInitBlock
= (const CFragInitBlock
*)initBlock
;
748 FSSpec
*fileSpec
= NULL
;
750 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
751 fileSpec
= theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
;
753 else if (theInitBlock
->fragLocator
.where
== kResourceCFragLocator
) {
754 fileSpec
= theInitBlock
->fragLocator
.u
.inSegs
.fileSpec
;
757 if (fileSpec
!= NULL
) {
758 gSharedLibraryResource
= FSpOpenResFile(fileSpec
, fsRdPerm
);
763 // Open the shared library resource file if it is not yet open
766 const char *theLibPath
;
768 gSharedLibraryBundle
= CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWidgets"));
769 if (gSharedLibraryBundle
!= NULL
) {
770 // wxWidgets has been bundled into a framework
771 // load the framework resources
773 gSharedLibraryResource
= CFBundleOpenBundleResourceMap(gSharedLibraryBundle
);
776 // wxWidgets is a simple dynamic shared library
777 // load the resources from the data fork of a separate resource file
781 OSErr theErr
= noErr
;
783 // get the library path
784 theSymbol
= NSLookupAndBindSymbol("_gSharedLibraryResource");
785 theModule
= NSModuleForSymbol(theSymbol
);
786 theLibPath
= NSLibraryNameForModule(theModule
);
788 // if we call wxLogDebug from here then, as wxTheApp hasn't been
789 // created yet when we're called from wxApp::Initialize(), wxLog
790 // is going to create a default stderr-based log target instead of
791 // the expected normal GUI one -- don't do it, if we really want
792 // to see this message just use fprintf() here
794 wxLogDebug( wxT("wxMac library installation name is '%s'"),
798 // allocate copy to replace .dylib.* extension with .rsrc
799 if (theLibPath
!= NULL
) {
801 theResPath
= wxString(theLibPath
, wxConvLocal
);
803 theResPath
= wxString(theLibPath
);
805 // replace '_core' with '' in case of multi-lib build
806 theResPath
.Replace(wxT("_core"), wxEmptyString
);
807 // replace ".dylib" shared library extension with ".rsrc"
808 theResPath
.Replace(wxT(".dylib"), wxT(".rsrc"));
809 // Find the begining of the filename
810 theName
= theResPath
.AfterLast('/');
813 wxLogDebug( wxT("wxMac resources file name is '%s'"),
814 theResPath
.mb_str() );
817 theErr
= FSPathMakeRef((UInt8
*) theResPath
.mb_str(), &theResRef
, false);
818 if (theErr
!= noErr
) {
819 // try in current directory (using name only)
820 theErr
= FSPathMakeRef((UInt8
*) theName
.mb_str(), &theResRef
, false);
823 // open the resource file
824 if (theErr
== noErr
) {
825 theErr
= FSOpenResourceFile( &theResRef
, 0, NULL
, fsRdPerm
,
826 &gSharedLibraryResource
);
828 if (theErr
!= noErr
) {
830 wxLogDebug( wxT("unable to open wxMac resource file '%s'\n"),
831 theResPath
.mb_str() );
832 #endif // __WXDEBUG__
837 #endif /* __DARWIN__ */
839 #endif /* WXMAKINGDLL_CORE */
842 void wxStAppResource::CloseSharedLibraryResource()
844 #ifdef WXMAKINGDLL_CORE
845 // Close the shared library resource file
846 if (gSharedLibraryResource
!= kResFileNotOpened
) {
848 if (gSharedLibraryBundle
!= NULL
) {
849 CFBundleCloseBundleResourceMap(gSharedLibraryBundle
,
850 gSharedLibraryResource
);
851 gSharedLibraryBundle
= NULL
;
854 #endif /* __DARWIN__ */
856 CloseResFile(gSharedLibraryResource
);
858 gSharedLibraryResource
= kResFileNotOpened
;
860 #endif /* WXMAKINGDLL_CORE */
863 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
865 // for shared libraries we have to manually get the correct resource
866 // ref num upon initializing and releasing when terminating, therefore
867 // the __wxinitialize and __wxterminate must be used
870 void __sinit(void); /* (generated by linker) */
871 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
872 pascal void __terminate(void);
875 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
877 wxStAppResource::OpenSharedLibraryResource( theInitBlock
) ;
878 return __initialize( theInitBlock
) ;
881 pascal void __wxterminate(void)
883 wxStAppResource::CloseSharedLibraryResource() ;
887 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
889 bool wxMacConvertEventToRecord( EventRef event
, EventRecord
*rec
)
891 bool converted
= ConvertEventRefToEventRecord( event
,rec
) ;
892 OSStatus err
= noErr
;
895 switch( GetEventClass( event
) )
897 case kEventClassKeyboard
:
900 switch( GetEventKind(event
) )
902 case kEventRawKeyDown
:
903 rec
->what
= keyDown
;
905 case kEventRawKeyRepeat
:
906 rec
->what
= autoKey
;
908 case kEventRawKeyUp
:
911 case kEventRawKeyModifiersChanged
:
912 rec
->what
= nullEvent
;
921 unsigned char charCode
;
923 GetMouse( &rec
->where
) ;
925 err
= GetEventParameter(event
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
926 err
= GetEventParameter(event
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
927 err
= GetEventParameter(event
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
928 rec
->modifiers
= modifiers
;
929 rec
->message
= (keyCode
<< 8 ) + charCode
;
933 case kEventClassTextInput
:
935 switch( GetEventKind( event
) )
937 case kEventTextInputUnicodeForKeyEvent
:
940 err
= GetEventParameter( event
, kEventParamTextInputSendKeyboardEvent
,typeEventRef
,NULL
,sizeof(rawEvent
),NULL
,&rawEvent
) ;
944 unsigned char charCode
;
946 GetMouse( &rec
->where
) ;
947 rec
->what
= keyDown
;
948 err
= GetEventParameter(rawEvent
, kEventParamKeyModifiers
, typeUInt32
, NULL
, 4, NULL
, &modifiers
);
949 err
= GetEventParameter(rawEvent
, kEventParamKeyCode
, typeUInt32
, NULL
, 4, NULL
, &keyCode
);
950 err
= GetEventParameter(rawEvent
, kEventParamKeyMacCharCodes
, typeChar
, NULL
, 1, NULL
, &charCode
);
951 rec
->modifiers
= modifiers
;
952 rec
->message
= (keyCode
<< 8 ) + charCode
;
969 m_printMode
= wxPRINT_WINDOWS
;
971 m_macCurrentEvent
= NULL
;
972 m_macCurrentEventHandlerCallRef
= NULL
;
975 int wxApp::MainLoop()
979 RunApplicationEventLoop() ;
989 void wxApp::ExitMainLoop()
993 QuitApplicationEventLoop() ;
997 // Is a message/event pending?
998 bool wxApp::Pending()
1000 // without the receive event (with pull param = false ) nothing is ever reported
1002 ReceiveNextEvent (0, NULL
, kEventDurationNoWait
, false, &theEvent
);
1003 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1006 // Dispatch a message.
1007 bool wxApp::Dispatch()
1014 void wxApp::OnIdle(wxIdleEvent
& event
)
1016 wxAppBase::OnIdle(event
);
1018 // If they are pending events, we must process them: pending events are
1019 // either events to the threads other than main or events posted with
1020 // wxPostEvent() functions
1021 wxMacProcessNotifierAndPendingEvents();
1023 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1024 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1027 void wxApp::WakeUpIdle()
1038 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1041 GetTopWindow()->Close(TRUE
);
1044 // Default behaviour: close the application with prompts. The
1045 // user can veto the close, and therefore the end session.
1046 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1050 if (!GetTopWindow()->Close(!event
.CanVeto()))
1055 extern "C" void wxCYield() ;
1061 // Yield to other processes
1063 bool wxApp::Yield(bool onlyIfNeeded
)
1067 if ( !onlyIfNeeded
)
1069 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1077 // by definition yield should handle all non-processed events
1081 OSStatus status
= noErr
;
1084 s_inReceiveEvent
= true ;
1085 status
= ReceiveNextEvent(0, NULL
,kEventDurationNoWait
,true,&theEvent
) ;
1086 s_inReceiveEvent
= false ;
1088 if ( status
== eventLoopTimedOutErr
)
1090 // make sure next time the event loop will trigger idle events
1091 sleepTime
= kEventDurationNoWait
;
1093 else if ( status
== eventLoopQuitErr
)
1095 // according to QA1061 this may also occur when a WakeUp Process
1100 MacHandleOneEvent( theEvent
) ;
1101 ReleaseEvent(theEvent
);
1103 } while( status
== noErr
) ;
1105 wxMacProcessNotifierAndPendingEvents() ;
1111 void wxApp::MacDoOneEvent()
1115 s_inReceiveEvent
= true ;
1116 OSStatus status
= ReceiveNextEvent(0, NULL
,sleepTime
,true,&theEvent
) ;
1117 s_inReceiveEvent
= false ;
1118 if ( status
== eventLoopTimedOutErr
)
1120 if ( wxTheApp
->ProcessIdle() )
1121 sleepTime
= kEventDurationNoWait
;
1123 sleepTime
= kEventDurationSecond
;
1125 else if ( status
== eventLoopQuitErr
)
1127 // according to QA1061 this may also occur when a WakeUp Process
1132 MacHandleOneEvent( theEvent
) ;
1133 ReleaseEvent(theEvent
);
1134 sleepTime
= kEventDurationNoWait
;
1138 DeletePendingObjects() ;
1139 wxMacProcessNotifierAndPendingEvents() ;
1142 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr
)
1144 // Override to process unhandled events as you please
1147 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1149 EventTargetRef theTarget
;
1150 theTarget
= GetEventDispatcherTarget();
1151 m_macCurrentEvent
= evr
;
1152 OSStatus status
= SendEventToEventTarget ((EventRef
) evr
, theTarget
);
1153 if(status
== eventNotHandledErr
)
1155 MacHandleUnhandledEvent(evr
);
1157 wxMacProcessNotifierAndPendingEvents() ;
1159 wxMutexGuiLeaveOrEnter();
1160 #endif // wxUSE_THREADS
1163 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1164 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1169 case kHomeCharCode
:
1172 case kEnterCharCode
:
1173 retval
= WXK_RETURN
;
1178 case kHelpCharCode
:
1181 case kBackspaceCharCode
:
1187 case kPageUpCharCode
:
1188 retval
= WXK_PAGEUP
;
1190 case kPageDownCharCode
:
1191 retval
= WXK_PAGEDOWN
;
1193 case kReturnCharCode
:
1194 retval
= WXK_RETURN
;
1196 case kFunctionKeyCharCode
:
1248 case kEscapeCharCode
:
1249 retval
= WXK_ESCAPE
;
1251 case kLeftArrowCharCode
:
1254 case kRightArrowCharCode
:
1255 retval
= WXK_RIGHT
;
1257 case kUpArrowCharCode
:
1260 case kDownArrowCharCode
:
1263 case kDeleteCharCode
:
1264 retval
= WXK_DELETE
;
1272 int wxMacKeyCodeToModifier(wxKeyCode key
)
1297 bool wxGetKeyState(wxKeyCode key
) //virtual key code if < 10.2.x, else see below
1300 // wxHIDKeyboard keyboard;
1301 // return keyboard.IsActive(key);
1303 // TODO: Have it use HID Manager on OSX...
1304 //if OS X > 10.2 (i.e. 10.2.x)
1305 //a known apple bug prevents the system from determining led
1306 //states with GetKeys... can only determine caps lock led
1307 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key
));
1309 // KeyMapByteArray keymap;
1310 // GetKeys((BigEndianLong*)keymap);
1311 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1316 bool wxApp::MacSendKeyDownEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1323 keychar
= short(keymessage
& charCodeMask
);
1324 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1326 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1328 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1329 // and look at the character after
1331 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1332 keychar
= short(keyInfo
& charCodeMask
);
1333 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1335 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1336 long realkeyval
= keyval
;
1337 if ( keyval
== keychar
)
1339 // 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)
1340 realkeyval
= short(keymessage
& charCodeMask
) ;
1341 keyval
= wxToupper( keyval
) ;
1344 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1345 bool handled
= false ;
1346 event
.m_shiftDown
= modifiers
& shiftKey
;
1347 event
.m_controlDown
= modifiers
& controlKey
;
1348 event
.m_altDown
= modifiers
& optionKey
;
1349 event
.m_metaDown
= modifiers
& cmdKey
;
1350 event
.m_keyCode
= keyval
;
1354 event
.m_timeStamp
= when
;
1355 event
.SetEventObject(focus
);
1356 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1357 if ( handled
&& event
.GetSkipped() )
1364 wxWindow
*ancestor
= focus
;
1367 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
1370 wxCommandEvent
command_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
1371 handled
= ancestor
->GetEventHandler()->ProcessEvent( command_event
);
1374 if (ancestor
->IsTopLevel())
1376 ancestor
= ancestor
->GetParent();
1379 #endif // wxUSE_ACCEL
1383 event
.Skip( FALSE
) ;
1384 event
.SetEventType( wxEVT_CHAR
) ;
1386 event
.m_keyCode
= realkeyval
;
1388 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1389 if ( handled
&& event
.GetSkipped() )
1392 if ( !handled
&& (keyval
== WXK_TAB
) )
1394 wxWindow
* iter
= focus
->GetParent() ;
1395 while( iter
&& !handled
)
1397 if ( iter
->HasFlag( wxTAB_TRAVERSAL
) )
1399 wxNavigationKeyEvent new_event
;
1400 new_event
.SetEventObject( focus
);
1401 new_event
.SetDirection( !event
.ShiftDown() );
1402 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1403 new_event
.SetWindowChange( event
.ControlDown() );
1404 new_event
.SetCurrentFocus( focus
);
1405 handled
= focus
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
1406 if ( handled
&& new_event
.GetSkipped() )
1409 iter
= iter
->GetParent() ;
1412 // backdoor handler for default return and command escape
1413 if ( !handled
&& (!focus
->IsKindOf(CLASSINFO(wxControl
) ) || !focus
->MacCanFocus() ) )
1415 // if window is not having a focus still testing for default enter or cancel
1416 // TODO add the UMA version for ActiveNonFloatingWindow
1417 wxWindow
* focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1420 if ( keyval
== WXK_RETURN
)
1422 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1424 if ( def
&& def
->IsEnabled() )
1426 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1427 event
.SetEventObject(def
);
1428 def
->Command(event
);
1432 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1433 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && modifiers
& cmdKey
) )
1435 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1436 new_event
.SetEventObject( focus
);
1437 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1444 bool wxApp::MacSendKeyUpEvent( wxWindow
* focus
, long keymessage
, long modifiers
, long when
, short wherex
, short wherey
)
1451 keychar
= short(keymessage
& charCodeMask
);
1452 keycode
= short(keymessage
& keyCodeMask
) >> 8 ;
1453 if ( modifiers
& ( controlKey
|shiftKey
|optionKey
) )
1455 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1456 // and look at the character after
1458 UInt32 keyInfo
= KeyTranslate((Ptr
)GetScriptManagerVariable(smKCHRCache
), ( modifiers
& (~(controlKey
|shiftKey
|optionKey
))) | keycode
, &state
);
1459 keychar
= short(keyInfo
& charCodeMask
);
1460 keycode
= short(keyInfo
& keyCodeMask
) >> 8 ;
1462 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1464 if ( keyval
== keychar
)
1466 keyval
= wxToupper( keyval
) ;
1468 bool handled
= false ;
1470 wxKeyEvent
event(wxEVT_KEY_UP
);
1471 event
.m_shiftDown
= modifiers
& shiftKey
;
1472 event
.m_controlDown
= modifiers
& controlKey
;
1473 event
.m_altDown
= modifiers
& optionKey
;
1474 event
.m_metaDown
= modifiers
& cmdKey
;
1475 event
.m_keyCode
= keyval
;
1479 event
.m_timeStamp
= when
;
1480 event
.SetEventObject(focus
);
1481 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;