1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
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 #if wxUSE_WX_RESOURCES
40 # include "wx/resource.h"
55 #include "wx/mac/uma.h"
56 #include "wx/mac/macnotfy.h"
59 # include <CoreServices/CoreServices.h>
63 # include <ToolUtils.h>
64 # include <DiskInit.h>
68 extern char *wxBuffer
;
69 extern wxList wxPendingDelete
;
70 extern wxList
*wxWinMacWindowList
;
71 extern wxList
*wxWinMacControlList
;
73 wxApp
*wxTheApp
= NULL
;
75 #if !USE_SHARED_LIBRARY
76 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
77 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
78 EVT_IDLE(wxApp::OnIdle
)
79 EVT_END_SESSION(wxApp::OnEndSession
)
80 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
85 const short kMacMinHeap
= (29 * 1024) ;
86 // platform specific static variables
88 const short kwxMacMenuBarResource
= 1 ;
89 const short kwxMacAppleMenuId
= 1 ;
91 WXHRGN
wxApp::s_macCursorRgn
= NULL
;
92 wxWindow
* wxApp::s_captureWindow
= NULL
;
93 int wxApp::s_lastMouseDown
= 0 ;
94 long wxApp::sm_lastMessageTime
= 0;
96 bool wxApp::s_macDefaultEncodingIsPC
= true ;
97 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
98 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
99 wxString
wxApp::s_macHelpMenuTitleName
= "&Help" ;
101 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
102 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
103 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
104 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
107 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
)
109 wxApp
* app
= (wxApp
*) refcon
;
110 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
113 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
)
115 wxApp
* app
= (wxApp
*) refcon
;
116 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
119 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
)
121 wxApp
* app
= (wxApp
*) refcon
;
122 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
125 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
)
127 wxApp
* app
= (wxApp
*) refcon
;
128 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
131 short wxApp::MacHandleAEODoc(const WXEVENTREF event
, WXEVENTREF reply
)
134 ProcessSerialNumber PSN
;
135 PSN
.highLongOfPSN
= 0 ;
136 PSN
.lowLongOfPSN
= kCurrentProcess
;
137 SetFrontProcess( &PSN
) ;
141 short wxApp::MacHandleAEPDoc(const WXEVENTREF event
, WXEVENTREF reply
)
146 short wxApp::MacHandleAEOApp(const WXEVENTREF event
, WXEVENTREF reply
)
151 short wxApp::MacHandleAEQuit(const WXEVENTREF event
, WXEVENTREF reply
)
153 wxWindow
* win
= GetTopWindow() ;
165 char StringMac
[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
166 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
167 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf"
168 "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf"
169 "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf"
170 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ;
172 char StringANSI
[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8"
173 "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC"
174 "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8"
175 "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8"
176 "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C"
177 "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ;
179 void wxMacConvertFromPC( const char *from
, char *to
, int len
)
184 for( int i
= 0 ; i
< len
; ++ i
)
186 c
= strchr( StringANSI
, *from
) ;
189 *to
= StringMac
[ c
- StringANSI
] ;
197 for( int i
= 0 ; i
< len
; ++ i
)
199 c
= strchr( StringANSI
, *from
) ;
202 *to
= StringMac
[ c
- StringANSI
] ;
214 void wxMacConvertToPC( const char *from
, char *to
, int len
)
219 for( int i
= 0 ; i
< len
; ++ i
)
221 c
= strchr( StringMac
, *from
) ;
224 *to
= StringANSI
[ c
- StringMac
] ;
232 for( int i
= 0 ; i
< len
; ++ i
)
234 c
= strchr( StringMac
, *from
) ;
237 *to
= StringANSI
[ c
- StringMac
] ;
249 void wxMacConvertFromPC( char * p
)
252 int len
= strlen ( p
) ;
254 wxMacConvertFromPC( ptr
, ptr
, len
) ;
257 void wxMacConvertFromPCForControls( char * p
)
260 int len
= strlen ( p
) ;
262 wxMacConvertFromPC( ptr
, ptr
, len
) ;
263 for ( int i
= 0 ; i
< strlen ( ptr
) ; i
++ )
265 if ( ptr
[i
] == '&' && ptr
[i
]+1 != ' ' )
267 memmove( &ptr
[i
] , &ptr
[i
+1] , strlen( &ptr
[i
+1] ) + 1) ;
272 void wxMacConvertFromPC( unsigned char *p
)
274 char *ptr
= (char*) p
+ 1 ;
277 wxMacConvertFromPC( ptr
, ptr
, len
) ;
280 extern char *wxBuffer
;
282 wxString
wxMacMakeMacStringFromPC( const char * p
)
284 const char *ptr
= p
;
285 int len
= strlen ( p
) ;
286 char *buf
= wxBuffer
;
288 if ( len
>= BUFSIZ
+ 512 )
290 buf
= new char [len
+1] ;
293 wxMacConvertFromPC( ptr
, buf
, len
) ;
295 wxString
result( buf
) ;
296 if ( buf
!= wxBuffer
)
302 void wxMacConvertToPC( char * p
)
305 int len
= strlen ( p
) ;
307 wxMacConvertToPC( ptr
, ptr
, len
) ;
310 void wxMacConvertToPC( unsigned char *p
)
312 char *ptr
= (char*) p
+ 1 ;
315 wxMacConvertToPC( ptr
, ptr
, len
) ;
318 wxString
wxMacMakePCStringFromMac( const char * p
)
320 const char *ptr
= p
;
321 int len
= strlen ( p
) ;
322 char *buf
= wxBuffer
;
324 if ( len
>= BUFSIZ
+ 512 )
326 buf
= new char [len
+1] ;
329 wxMacConvertToPC( ptr
, buf
, len
) ;
332 wxString
result( buf
) ;
333 if ( buf
!= wxBuffer
)
338 wxString
wxMacMakeStringFromMacString( const char* from
, bool mac2pcEncoding
)
342 return wxMacMakePCStringFromMac( from
) ;
346 return wxString( from
) ;
350 wxString
wxMacMakeStringFromPascal( StringPtr from
, bool mac2pcEncoding
)
352 // this is safe since a pascal string can never be larger than 256 bytes
354 CopyPascalStringToC( from
, s
) ;
357 return wxMacMakePCStringFromMac( s
) ;
361 return wxString( s
) ;
365 void wxMacStringToPascal( const char * from
, StringPtr to
, bool pc2macEncoding
)
369 CopyCStringToPascal( wxMacMakeMacStringFromPC( from
) , to
) ;
373 CopyCStringToPascal( from
, to
) ;
378 // we know it's there ;-)
379 WXIMPORT
char std::__throws_bad_alloc
;
383 bool wxApp::Initialize()
389 UMAInitToolbox( 4 ) ;
390 SetEventMask( everyEvent
) ;
391 UMAShowWatchCursor() ;
393 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
394 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
, NewAEEventHandlerUPP(AEHandleODoc
) ,
395 (long) wxTheApp
, FALSE
) ;
396 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
, NewAEEventHandlerUPP(AEHandleOApp
) ,
397 (long) wxTheApp
, FALSE
) ;
398 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
, NewAEEventHandlerUPP(AEHandlePDoc
) ,
399 (long) wxTheApp
, FALSE
) ;
400 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
, NewAEEventHandlerUPP(AEHandleQuit
) ,
401 (long) wxTheApp
, FALSE
) ;
403 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
, NewAEEventHandlerProc(AEHandleODoc
) ,
404 (long) wxTheApp
, FALSE
) ;
405 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
, NewAEEventHandlerProc(AEHandleOApp
) ,
406 (long) wxTheApp
, FALSE
) ;
407 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
, NewAEEventHandlerProc(AEHandlePDoc
) ,
408 (long) wxTheApp
, FALSE
) ;
409 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
, NewAEEventHandlerProc(AEHandleQuit
) ,
410 (long) wxTheApp
, FALSE
) ;
415 // test the minimal configuration necessary
421 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
423 error
= kMacSTRWrongMachine
;
425 else if (theMachine
< gestaltMacPlus
)
427 error
= kMacSTRWrongMachine
;
429 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
431 error
= kMacSTROldSystem
;
433 else if ( theSystem
< 0x0860 )
435 error
= kMacSTROldSystem
;
437 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
439 error
= kMacSTRSmallSize
;
445 if ( !UMAHasAppearance() )
447 error = kMacSTRNoPre8Yet ;
453 // if we encountered any problems so far, give the error code and exit immediately
457 wxStAppResource resload
;
461 GetIndString(message
, 128, error
);
462 UMAShowArrowCursor() ;
463 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
464 itemHit
= Alert(128, nil
);
469 #if __option(profile)
470 ProfilerInit( collectDetailed
, bestTimeBase
, 20000 , 40 ) ;
474 // now avoid exceptions thrown for new (bad_alloc)
477 std::__throws_bad_alloc
= FALSE
;
480 s_macCursorRgn
= ::NewRgn() ;
483 wxBuffer
= new char[1500];
485 wxBuffer
= new char[BUFSIZ
+ 512];
488 wxClassInfo::InitializeClasses();
491 // wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
495 wxPendingEventsLocker
= new wxCriticalSection
;
497 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
498 wxTheColourDatabase
->Initialize();
502 // flush the logged messages if any and install a 'safer' log target: the
503 // default one (wxLogGui) can't be used after the resources are freed just
504 // below and the user suppliedo ne might be even more unsafe (using any
505 // wxWindows GUI function is unsafe starting from now)
506 wxLog::DontCreateOnDemand();
508 // this will flush the old messages if any
509 delete wxLog::SetActiveTarget(new wxLogStderr
);
513 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
514 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
516 wxInitializeStockLists();
517 wxInitializeStockObjects();
519 #if wxUSE_WX_RESOURCES
520 wxInitializeResourceSystem();
523 wxBitmap::InitStandardHandlers();
525 wxModule::RegisterModules();
526 if (!wxModule::InitializeModules()) {
530 wxMacCreateNotifierTable() ;
532 UMAShowArrowCursor() ;
537 void wxApp::CleanUp()
539 wxToolTip::RemoveToolTips() ;
541 // flush the logged messages if any and install a 'safer' log target: the
542 // default one (wxLogGui) can't be used after the resources are freed just
543 // below and the user suppliedo ne might be even more unsafe (using any
544 // wxWindows GUI function is unsafe starting from now)
545 wxLog::DontCreateOnDemand();
547 // this will flush the old messages if any
548 delete wxLog::SetActiveTarget(new wxLogStderr
);
551 // One last chance for pending objects to be cleaned up
552 wxTheApp
->DeletePendingObjects();
554 wxModule::CleanUpModules();
556 #if wxUSE_WX_RESOURCES
557 wxCleanUpResourceSystem();
560 wxDeleteStockObjects() ;
562 // Destroy all GDI lists, etc.
563 wxDeleteStockLists();
565 delete wxTheColourDatabase
;
566 wxTheColourDatabase
= NULL
;
568 wxBitmap::CleanUpHandlers();
573 wxMacDestroyNotifierTable() ;
574 if (wxWinMacWindowList
)
575 delete wxWinMacWindowList
;
577 if (wxWinMacControlList
)
578 delete wxWinMacControlList
;
580 delete wxPendingEvents
;
582 delete wxPendingEventsLocker
;
583 // If we don't do the following, we get an apparent memory leak.
584 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
587 wxClassInfo::CleanUpClasses();
590 #if __option(profile)
591 ProfilerDump( "\papp.prof" ) ;
599 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
600 // At this point we want to check if there are any memory
601 // blocks that aren't part of the wxDebugContext itself,
602 // as a special case. Then when dumping we need to ignore
603 // wxDebugContext, too.
604 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
606 wxLogDebug(wxT("There were memory leaks."));
607 wxDebugContext::Dump();
608 wxDebugContext::PrintStatistics();
610 // wxDebugContext::SetStream(NULL, NULL);
614 // do it as the very last thing because everything else can log messages
615 delete wxLog::SetActiveTarget(NULL
);
618 UMACleanupToolbox() ;
620 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
627 //----------------------------------------------------------------------
629 //----------------------------------------------------------------------
631 short gCurrentResource
= -1 ;
633 wxStAppResource::wxStAppResource()
635 m_currentRefNum
= CurResFile() ;
636 if ( gCurrentResource
!= -1 )
638 UseResFile( gCurrentResource
) ;
642 wxStAppResource::~wxStAppResource()
644 if ( m_currentRefNum
!= -1 )
646 UseResFile( m_currentRefNum
) ;
652 // for shared libraries we have to manually get the correct resource ref num upon
653 // initializing and releasing when terminating, therefore the __wxinitialize and __wxterminate
660 void __sinit(void); /* (generated by linker) */
661 pascal OSErr
__initialize(const CFragInitBlock
*theInitBlock
);
662 pascal void __terminate(void);
668 pascal OSErr
__wxinitialize(const CFragInitBlock
*theInitBlock
)
670 gCurrentResource
= -1;
672 if (theInitBlock
->fragLocator
.where
== kDataForkCFragLocator
) {
674 FSpOpenResFile(theInitBlock
->fragLocator
.u
.onDisk
.fileSpec
,
677 return __initialize( theInitBlock
) ;
680 pascal void __wxterminate(void)
682 if (gCurrentResource
!= -1)
683 CloseResFile(gCurrentResource
);
688 int WXDLLEXPORT
wxEntryStart( int argc
, char *argv
[] )
690 return wxApp::Initialize();
694 int WXDLLEXPORT
wxEntryInitGui()
696 return wxTheApp
->OnInitGui();
700 void WXDLLEXPORT
wxEntryCleanup()
706 int wxEntry( int argc
, char *argv
[] , bool enterLoop
)
708 gCurrentResource
= CurResFile() ;
710 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
711 // This seems to be necessary since there are 'rogue'
712 // objects present at this point (perhaps global objects?)
713 // Setting a checkpoint will ignore them as far as the
714 // memory checking facility is concerned.
715 // Of course you may argue that memory allocated in globals should be
716 // checked, but this is a reasonable compromise.
717 wxDebugContext::SetCheckpoint();
720 if (!wxEntryStart(argc
, argv
)) {
723 // create the application object or ensure that one already exists
726 // The app may have declared a global application object, but we recommend
727 // the IMPLEMENT_APP macro is used instead, which sets an initializer
728 // function for delayed, dynamic app object construction.
729 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
730 wxT("No initializer - use IMPLEMENT_APP macro.") );
732 wxTheApp
= (wxApp
*) (*wxApp::GetInitializerFunction()) ();
735 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
738 argc
= 0 ; // currently we don't support files as parameters
740 // we could try to get the open apple events here to adjust argc and argv better
742 wxTheApp
->argc
= argc
;
743 wxTheApp
->argv
= argv
;
745 // GUI-specific initialization, such as creating an app context.
749 // Here frames insert themselves automatically
750 // into wxTopLevelWindows by getting created
755 if ( wxTheApp
->OnInit() )
759 retValue
= wxTheApp
->OnRun();
762 // We want to initialize, but not run or exit immediately.
765 //else: app initialization failed, so we skipped OnRun()
767 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
770 // Forcibly delete the window.
771 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
772 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
774 topWindow
->Close(TRUE
);
775 wxTheApp
->DeletePendingObjects();
780 wxTheApp
->SetTopWindow(NULL
);
791 // Static member initialization
792 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
799 #if WXWIN_COMPATIBILITY_2_2
800 m_wantDebugOutput
= TRUE
;
806 m_printMode
= wxPRINT_WINDOWS
;
807 m_exitOnFrameDelete
= TRUE
;
811 bool wxApp::Initialized()
819 int wxApp::MainLoop()
831 // Returns TRUE if more time is needed.
832 bool wxApp::ProcessIdle()
835 event
.SetEventObject(this);
838 return event
.MoreRequested();
841 void wxApp::ExitMainLoop()
846 // Is a message/event pending?
847 bool wxApp::Pending()
851 return EventAvail( everyEvent
, &event
) ;
854 // Dispatch a message.
855 void wxApp::Dispatch()
860 void wxApp::OnIdle(wxIdleEvent
& event
)
862 static bool s_inOnIdle
= FALSE
;
864 // Avoid recursion (via ProcessEvent default case)
871 // 'Garbage' collection of windows deleted with Close().
872 DeletePendingObjects();
874 // flush the logged messages if any
875 wxLog
*pLog
= wxLog::GetActiveTarget();
876 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
879 // Send OnIdle events to all windows
880 bool needMore
= SendIdleEvents();
883 event
.RequestMore(TRUE
);
885 // If they are pending events, we must process them: pending events are
886 // either events to the threads other than main or events posted with
887 // wxPostEvent() functions
888 wxMacProcessNotifierAndPendingEvents();
898 // Send idle event to all top-level windows
899 bool wxApp::SendIdleEvents()
901 bool needMore
= FALSE
;
902 wxNode
* node
= wxTopLevelWindows
.First();
905 wxWindow
* win
= (wxWindow
*) node
->Data();
906 if (SendIdleEvents(win
))
914 // Send idle event to window and all subwindows
915 bool wxApp::SendIdleEvents(wxWindow
* win
)
917 bool needMore
= FALSE
;
920 event
.SetEventObject(win
);
921 win
->ProcessEvent(event
);
923 if (event
.MoreRequested())
926 wxNode
* node
= win
->GetChildren().First();
929 wxWindow
* win
= (wxWindow
*) node
->Data();
930 if (SendIdleEvents(win
))
938 void wxApp::DeletePendingObjects()
940 wxNode
*node
= wxPendingDelete
.First();
943 wxObject
*obj
= (wxObject
*)node
->Data();
947 if (wxPendingDelete
.Member(obj
))
950 // Deleting one object may have deleted other pending
951 // objects, so start from beginning of list again.
952 node
= wxPendingDelete
.First();
958 wxLogError(_("Fatal error: exiting"));
964 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
967 GetTopWindow()->Close(TRUE
);
970 // Default behaviour: close the application with prompts. The
971 // user can veto the close, and therefore the end session.
972 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
976 if (!GetTopWindow()->Close(!event
.CanVeto()))
981 extern "C" void wxCYield() ;
987 // Yield to other processes
989 bool wxApp::Yield(bool onlyIfNeeded
)
991 static bool s_inYield
= FALSE
;
997 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1006 YieldToAnyThread() ;
1010 long sleepTime
= 1 ; //::GetCaretTime();
1012 while ( !wxTheApp
->IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
1014 wxTheApp
->MacHandleOneEvent( &event
);
1015 if ( event
.what
!= kHighLevelEvent
)
1016 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1019 wxMacProcessNotifierAndPendingEvents() ;
1026 // platform specifics
1028 void wxApp::MacSuspend( bool convertClipboard
)
1030 // we have to deactive the top level windows manually
1032 wxNode
* node
= wxTopLevelWindows
.First();
1035 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
1036 win
->MacActivate( MacGetCurrentEvent() , false ) ;
1038 node
= node
->Next();
1041 s_lastMouseDown
= 0 ;
1042 if( convertClipboard
)
1044 MacConvertPrivateToPublicScrap() ;
1047 ::HideFloatingWindows() ;
1050 void wxApp::MacResume( bool convertClipboard
)
1052 s_lastMouseDown
= 0 ;
1053 if( convertClipboard
)
1055 MacConvertPublicToPrivateScrap() ;
1058 ::ShowFloatingWindows() ;
1061 void wxApp::MacConvertPrivateToPublicScrap()
1065 void wxApp::MacConvertPublicToPrivateScrap()
1069 void wxApp::MacDoOneEvent()
1073 long sleepTime
= 1; // GetCaretTime() / 4 ;
1075 if (WaitNextEvent(everyEvent
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1077 MacHandleOneEvent( &event
);
1082 WindowPtr window
= ::FrontWindow() ;
1084 ::IdleControls( window
) ;
1086 wxTheApp
->ProcessIdle() ;
1088 if ( event
.what
!= kHighLevelEvent
)
1089 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1093 DeletePendingObjects() ;
1094 wxMacProcessNotifierAndPendingEvents() ;
1097 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1099 EventRecord
* ev
= (EventRecord
*) evr
;
1100 m_macCurrentEvent
= ev
;
1102 wxApp::sm_lastMessageTime
= ev
->when
;
1107 MacHandleMouseDownEvent( ev
) ;
1108 if ( ev
->modifiers
& controlKey
)
1109 s_lastMouseDown
= 2;
1111 s_lastMouseDown
= 1;
1114 if ( s_lastMouseDown
== 2 )
1116 ev
->modifiers
|= controlKey
;
1120 ev
->modifiers
&= ~controlKey
;
1122 MacHandleMouseUpEvent( ev
) ;
1123 s_lastMouseDown
= 0;
1126 MacHandleActivateEvent( ev
) ;
1129 MacHandleUpdateEvent( ev
) ;
1133 MacHandleKeyDownEvent( ev
) ;
1136 MacHandleKeyUpEvent( ev
) ;
1139 MacHandleDiskEvent( ev
) ;
1142 MacHandleOSEvent( ev
) ;
1144 case kHighLevelEvent
:
1145 MacHandleHighLevelEvent( ev
) ;
1150 wxMacProcessNotifierAndPendingEvents() ;
1153 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1155 EventRecord
* ev
= (EventRecord
*) evr
;
1156 ::AEProcessAppleEvent( ev
) ;
1159 bool s_macIsInModalLoop
= false ;
1161 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1163 EventRecord
* ev
= (EventRecord
*) evr
;
1164 wxToolTip::RemoveToolTips() ;
1167 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1168 WindowAttributes frontWindowAttributes
= NULL
;
1170 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1172 short windowPart
= ::FindWindow(ev
->where
, &window
);
1173 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1174 if ( wxPendingDelete
.Member(win
) )
1178 GetQDGlobalsScreenBits( &screenBits
);
1183 if ( s_macIsInModalLoop
)
1189 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1190 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1191 s_lastMouseDown
= 0;
1196 SystemClick( ev
, window
) ;
1197 s_lastMouseDown
= 0;
1201 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1207 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1212 Point pt
= { 0, 0 } ;
1213 SetPortWindowPort(window
) ;
1214 LocalToGlobal( &pt
) ;
1216 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1217 -1 , wxSIZE_USE_EXISTING
);
1219 s_lastMouseDown
= 0;
1223 if (TrackGoAway(window
, ev
->where
))
1228 s_lastMouseDown
= 0;
1232 int growResult
= GrowWindow(window
, ev
->where
, &screenBits
.bounds
);
1233 if (growResult
!= 0)
1235 int newWidth
= LoWord(growResult
);
1236 int newHeight
= HiWord(growResult
);
1237 int oldWidth
, oldHeight
;
1242 win
->GetSize(&oldWidth
, &oldHeight
);
1244 newWidth
= oldWidth
;
1246 newHeight
= oldHeight
;
1247 win
->SetSize( -1, -1, newWidth
, newHeight
, wxSIZE_USE_EXISTING
);
1250 s_lastMouseDown
= 0;
1255 if (TrackBox(window
, ev
->where
, windowPart
))
1257 // TODO setup size event
1258 ZoomWindow( window
, windowPart
, false ) ;
1263 GetWindowPortBounds(window
, &tempRect
) ;
1264 win
->SetSize( -1, -1, tempRect
.right
-tempRect
.left
,
1265 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1268 s_lastMouseDown
= 0;
1270 case inCollapseBox
:
1271 // TODO setup size event
1272 s_lastMouseDown
= 0;
1279 SetPortWindowPort(window
) ;
1282 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1284 if ( s_macIsInModalLoop
)
1288 else if ( UMAIsWindowFloating( window
) )
1291 win
->MacMouseDown( ev
, windowPart
) ;
1296 win
->MacMouseDown( ev
, windowPart
) ;
1297 ::SelectWindow( window
) ;
1303 win
->MacMouseDown( ev
, windowPart
) ;
1312 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1314 EventRecord
* ev
= (EventRecord
*) evr
;
1317 short windowPart
= inNoWindow
;
1318 if ( wxTheApp
->s_captureWindow
)
1320 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1321 windowPart
= inContent
;
1325 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1336 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1338 win
->MacMouseUp( ev
, windowPart
) ;
1344 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1345 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1354 retval
= WXK_RETURN
;
1369 retval
= WXK_PAGEUP
;
1372 retval
= WXK_PAGEDOWN
;
1375 retval
= WXK_RETURN
;
1430 retval
= WXK_ESCAPE
;
1436 retval
= WXK_RIGHT
;
1445 retval
= WXK_DELETE
;
1453 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1455 EventRecord
* ev
= (EventRecord
*) evr
;
1456 wxToolTip::RemoveToolTips() ;
1458 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1459 if ( HiWord( menuresult
) )
1461 if ( !s_macIsInModalLoop
)
1462 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1468 keychar
= short(ev
->message
& charCodeMask
);
1469 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1470 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1471 bool handled
= false ;
1472 wxWindow
* focus
= wxWindow::FindFocus() ;
1476 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1477 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1478 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1479 event
.m_altDown
= ev
->modifiers
& optionKey
;
1480 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1481 event
.m_keyCode
= keyval
;
1482 event
.m_x
= ev
->where
.h
;
1483 event
.m_y
= ev
->where
.v
;
1484 event
.m_timeStamp
= ev
->when
;
1485 event
.SetEventObject(focus
);
1486 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1492 wxWindow
*ancestor
= focus
;
1496 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1499 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1500 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1503 if (ancestor->m_isFrame)
1505 ancestor = ancestor->GetParent();
1509 #endif // wxUSE_ACCEL
1513 wxKeyEvent
event(wxEVT_CHAR
);
1514 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1515 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1516 event
.m_altDown
= ev
->modifiers
& optionKey
;
1517 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1518 event
.m_keyCode
= keyval
;
1519 event
.m_x
= ev
->where
.h
;
1520 event
.m_y
= ev
->where
.v
;
1521 event
.m_timeStamp
= ev
->when
;
1522 event
.SetEventObject(focus
);
1523 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1526 (keyval
== WXK_TAB
) &&
1527 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
1528 (focus
->GetParent()) &&
1529 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
1531 wxNavigationKeyEvent new_event
;
1532 new_event
.SetEventObject( focus
);
1533 new_event
.SetDirection( !event
.ShiftDown() );
1534 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1535 new_event
.SetWindowChange( event
.ControlDown() );
1536 new_event
.SetCurrentFocus( focus
);
1537 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1542 // if window is not having a focus still testing for default enter or cancel
1543 // TODO add the UMA version for ActiveNonFloatingWindow
1544 focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1547 if ( keyval
== WXK_RETURN
)
1549 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1551 if ( def
&& def
->IsEnabled() )
1553 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1554 event
.SetEventObject(def
);
1555 def
->Command(event
);
1559 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1560 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && ev
->modifiers
& cmdKey
) )
1562 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1563 new_event
.SetEventObject( focus
);
1564 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1571 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1573 EventRecord
* ev
= (EventRecord
*) evr
;
1574 wxToolTip::RemoveToolTips() ;
1576 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1577 if ( HiWord( menuresult
) )
1584 keychar
= short(ev
->message
& charCodeMask
);
1585 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1587 wxWindow
* focus
= wxWindow::FindFocus() ;
1590 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1592 wxKeyEvent
event(wxEVT_KEY_UP
);
1593 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1594 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1595 event
.m_altDown
= ev
->modifiers
& optionKey
;
1596 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1597 event
.m_keyCode
= keyval
;
1598 event
.m_x
= ev
->where
.h
;
1599 event
.m_y
= ev
->where
.v
;
1600 event
.m_timeStamp
= ev
->when
;
1601 event
.SetEventObject(focus
);
1602 bool handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1607 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
1609 EventRecord
* ev
= (EventRecord
*) evr
;
1610 WindowRef window
= (WindowRef
) ev
->message
;
1613 bool activate
= (ev
->modifiers
& activeFlag
) ;
1614 WindowClass wclass
;
1615 ::GetWindowClass ( window
, &wclass
) ;
1616 if ( wclass
== kFloatingWindowClass
)
1618 // if it is a floater we activate/deactivate the front non-floating window instead
1619 window
= ::FrontNonFloatingWindow() ;
1621 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1623 win
->MacActivate( ev
, activate
) ;
1627 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
1629 EventRecord
* ev
= (EventRecord
*) evr
;
1630 WindowRef window
= (WindowRef
) ev
->message
;
1631 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1634 if ( !wxPendingDelete
.Member(win
) )
1635 win
->MacUpdate( ev
->when
) ;
1639 // since there is no way of telling this foreign window to update itself
1640 // we have to invalidate the update region otherwise we keep getting the same
1641 // event over and over again
1642 BeginUpdate( window
) ;
1643 EndUpdate( window
) ;
1647 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
1649 EventRecord
* ev
= (EventRecord
*) evr
;
1650 if ( HiWord( ev
->message
) != noErr
)
1655 SetPt( &point
, 100 , 100 ) ;
1657 err
= DIBadMount( point
, ev
->message
) ;
1658 wxASSERT( err
== noErr
) ;
1663 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
1665 EventRecord
* ev
= (EventRecord
*) evr
;
1666 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
1668 case suspendResumeMessage
:
1670 bool isResuming
= ev
->message
& resumeFlag
;
1672 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
1674 bool convertClipboard
= false;
1676 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
1679 WindowRef oldFrontWindow
= NULL
;
1680 WindowRef newFrontWindow
= NULL
;
1682 // in case we don't take care of activating ourselves, we have to synchronize
1683 // our idea of the active window with the process manager's - which it already activated
1685 if ( !doesActivate
)
1686 oldFrontWindow
= ::FrontNonFloatingWindow() ;
1688 MacResume( convertClipboard
) ;
1690 newFrontWindow
= ::FrontNonFloatingWindow() ;
1692 if ( oldFrontWindow
)
1694 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
1696 win
->MacActivate( ev
, false ) ;
1698 if ( newFrontWindow
)
1700 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
1702 win
->MacActivate( ev
, true ) ;
1707 MacSuspend( convertClipboard
) ;
1709 // in case this suspending did close an active window, another one might
1710 // have surfaced -> lets deactivate that one
1712 /* TODO : find out what to do on systems < 10 , perhaps FrontNonFloatingWindow
1713 WindowRef newActiveWindow = ::ActiveNonFloatingWindow() ;
1714 if ( newActiveWindow )
1716 wxWindow* win = wxFindWinFromMacWindow( newActiveWindow ) ;
1718 win->MacActivate( ev , false ) ;
1724 case mouseMovedMessage
:
1728 wxWindow
* currentMouseWindow
= NULL
;
1730 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
1731 ¤tMouseWindow
) ;
1732 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
1734 wxMouseEvent event
;
1736 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
1737 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
1739 event
.m_leftDown
= isDown
&& !controlDown
;
1740 event
.m_middleDown
= FALSE
;
1741 event
.m_rightDown
= isDown
&& controlDown
;
1742 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1743 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1744 event
.m_altDown
= ev
->modifiers
& optionKey
;
1745 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1746 event
.m_x
= ev
->where
.h
;
1747 event
.m_y
= ev
->where
.v
;
1748 event
.m_timeStamp
= ev
->when
;
1749 event
.SetEventObject(this);
1751 if ( wxWindow::s_lastMouseWindow
)
1753 wxMouseEvent
eventleave(event
);
1754 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
1755 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
1757 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
1759 if ( currentMouseWindow
)
1761 wxMouseEvent
evententer(event
);
1762 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
1763 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
1765 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
1767 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
1770 short windowPart
= ::FindWindow(ev
->where
, &window
);
1774 // fixes for setting the cursor back from dominic mazzoni
1776 UMAShowArrowCursor();
1779 UMAShowArrowCursor();
1783 // if ( s_lastMouseDown == 0 )
1784 // ev->modifiers |= btnState ;
1786 // Calling GetNextEvent with a zero event mask will always
1787 // pass back a null event. However, it fills the EventRecord
1788 // with the state of the modifier keys. This is needed since
1789 // the modifier state returned by WaitForNextEvent often is
1790 // wrong mouse move events. The attempt above to correct this
1791 // didn't always work (under OS X at least).
1794 ::GetNextEvent(0, &tmp
);
1795 ev
->modifiers
= tmp
.modifiers
;
1797 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1799 win
->MacMouseMoved( ev
, windowPart
) ;
1801 UMAShowArrowCursor();
1812 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
1815 return; // no menu item selected
1817 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
1820 Str255 deskAccessoryName
;
1823 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
1824 GetPort(&savedPort
);
1825 OpenDeskAcc(deskAccessoryName
);
1831 wxWindow
* frontwindow
= wxFindWinFromMacWindow( ::FrontWindow() ) ;
1832 if ( frontwindow
&& wxMenuBar::MacGetInstalledMenuBar() )
1833 wxMenuBar::MacGetInstalledMenuBar()->MacMenuSelect( frontwindow
->GetEventHandler() , 0 , macMenuId
, macMenuItemNum
) ;
1839 long wxApp::MacTranslateKey(char key, int mods)
1843 void wxApp::MacAdjustCursor()
1850 wxApp::macAdjustCursor()
1852 if (ev->what != kHighLevelEvent)
1854 wxWindow* theMacWxFrame = wxFrame::MacFindFrameOrDialog(::FrontWindow());
1857 if (!theMacWxFrame->MacAdjustCursor(ev->where))
1858 ::SetCursor(&(qd.arrow));