1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "app.h"
16 #include "wx/window.h"
18 #include "wx/button.h"
21 #include "wx/gdicmn.h"
24 #include "wx/cursor.h"
27 #include "wx/palette.h"
29 #include "wx/dialog.h"
30 #include "wx/msgdlg.h"
32 #include "wx/module.h"
33 #include "wx/memory.h"
34 #include "wx/tooltip.h"
35 #include "wx/textctrl.h"
37 #if wxUSE_WX_RESOURCES
38 #include "wx/resource.h"
53 #include "wx/mac/uma.h"
54 #include "wx/mac/macnotfy.h"
58 #include <CoreServices/CoreServices.h>
62 #include <ToolUtils.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
) ;
377 bool wxApp::Initialize()
383 UMAInitToolbox( 4 ) ;
384 SetEventMask( everyEvent
) ;
385 UMAShowWatchCursor() ;
387 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
388 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
, NewAEEventHandlerUPP(AEHandleODoc
) ,
389 (long) wxTheApp
, FALSE
) ;
390 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
, NewAEEventHandlerUPP(AEHandleOApp
) ,
391 (long) wxTheApp
, FALSE
) ;
392 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
, NewAEEventHandlerUPP(AEHandlePDoc
) ,
393 (long) wxTheApp
, FALSE
) ;
394 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
, NewAEEventHandlerUPP(AEHandleQuit
) ,
395 (long) wxTheApp
, FALSE
) ;
397 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
, NewAEEventHandlerProc(AEHandleODoc
) ,
398 (long) wxTheApp
, FALSE
) ;
399 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
, NewAEEventHandlerProc(AEHandleOApp
) ,
400 (long) wxTheApp
, FALSE
) ;
401 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
, NewAEEventHandlerProc(AEHandlePDoc
) ,
402 (long) wxTheApp
, FALSE
) ;
403 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
, NewAEEventHandlerProc(AEHandleQuit
) ,
404 (long) wxTheApp
, FALSE
) ;
409 // test the minimal configuration necessary
415 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
417 error
= kMacSTRWrongMachine
;
419 else if (theMachine
< gestaltMacPlus
)
421 error
= kMacSTRWrongMachine
;
423 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
425 error
= kMacSTROldSystem
;
427 else if ( theSystem
< 0x0860 )
429 error
= kMacSTROldSystem
;
431 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
433 error
= kMacSTRSmallSize
;
439 if ( !UMAHasAppearance() )
441 error = kMacSTRNoPre8Yet ;
447 // if we encountered any problems so far, give the error code and exit immediately
454 GetIndString(message
, 128, error
);
455 UMAShowArrowCursor() ;
456 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
457 itemHit
= Alert(128, nil
);
462 #if __option(profile)
463 ProfilerInit( collectDetailed
, bestTimeBase
, 20000 , 40 ) ;
467 // now avoid exceptions thrown for new (bad_alloc)
470 std::__throws_bad_alloc
= FALSE
;
473 s_macCursorRgn
= ::NewRgn() ;
476 wxBuffer
= new char[1500];
478 wxBuffer
= new char[BUFSIZ
+ 512];
481 wxClassInfo::InitializeClasses();
484 // wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
488 wxPendingEventsLocker
= new wxCriticalSection
;
490 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
491 wxTheColourDatabase
->Initialize();
495 // flush the logged messages if any and install a 'safer' log target: the
496 // default one (wxLogGui) can't be used after the resources are freed just
497 // below and the user suppliedo ne might be even more unsafe (using any
498 // wxWindows GUI function is unsafe starting from now)
499 wxLog::DontCreateOnDemand();
501 // this will flush the old messages if any
502 delete wxLog::SetActiveTarget(new wxLogStderr
);
506 wxInitializeStockLists();
507 wxInitializeStockObjects();
509 #if wxUSE_WX_RESOURCES
510 wxInitializeResourceSystem();
513 wxBitmap::InitStandardHandlers();
515 wxModule::RegisterModules();
516 if (!wxModule::InitializeModules()) {
520 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
521 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
523 wxMacCreateNotifierTable() ;
525 UMAShowArrowCursor() ;
530 void wxApp::CleanUp()
532 wxToolTip::RemoveToolTips() ;
534 // flush the logged messages if any and install a 'safer' log target: the
535 // default one (wxLogGui) can't be used after the resources are freed just
536 // below and the user suppliedo ne might be even more unsafe (using any
537 // wxWindows GUI function is unsafe starting from now)
538 wxLog::DontCreateOnDemand();
540 // this will flush the old messages if any
541 delete wxLog::SetActiveTarget(new wxLogStderr
);
544 // One last chance for pending objects to be cleaned up
545 wxTheApp
->DeletePendingObjects();
547 wxModule::CleanUpModules();
549 #if wxUSE_WX_RESOURCES
550 wxCleanUpResourceSystem();
553 wxDeleteStockObjects() ;
555 // Destroy all GDI lists, etc.
556 wxDeleteStockLists();
558 delete wxTheColourDatabase
;
559 wxTheColourDatabase
= NULL
;
561 wxBitmap::CleanUpHandlers();
566 wxMacDestroyNotifierTable() ;
567 if (wxWinMacWindowList
)
568 delete wxWinMacWindowList
;
570 if (wxWinMacControlList
)
571 delete wxWinMacControlList
;
573 delete wxPendingEvents
;
575 delete wxPendingEventsLocker
;
576 // If we don't do the following, we get an apparent memory leak.
577 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
580 wxClassInfo::CleanUpClasses();
583 #if __option(profile)
584 ProfilerDump( "\papp.prof" ) ;
592 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
593 // At this point we want to check if there are any memory
594 // blocks that aren't part of the wxDebugContext itself,
595 // as a special case. Then when dumping we need to ignore
596 // wxDebugContext, too.
597 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
599 wxLogDebug(wxT("There were memory leaks."));
600 wxDebugContext::Dump();
601 wxDebugContext::PrintStatistics();
603 // wxDebugContext::SetStream(NULL, NULL);
607 // do it as the very last thing because everything else can log messages
608 delete wxLog::SetActiveTarget(NULL
);
611 UMACleanupToolbox() ;
613 ::DisposeRgn((RgnHandle
)s_macCursorRgn
);
620 //----------------------------------------------------------------------
622 //----------------------------------------------------------------------
624 int wxEntryStart( int argc
, char *argv
[] )
626 return wxApp::Initialize();
632 return wxTheApp
->OnInitGui();
636 void wxEntryCleanup()
642 int wxEntry( int argc
, char *argv
[] , bool enterLoop
)
645 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
646 // This seems to be necessary since there are 'rogue'
647 // objects present at this point (perhaps global objects?)
648 // Setting a checkpoint will ignore them as far as the
649 // memory checking facility is concerned.
650 // Of course you may argue that memory allocated in globals should be
651 // checked, but this is a reasonable compromise.
652 wxDebugContext::SetCheckpoint();
655 if (!wxEntryStart(argc
, argv
)) {
658 // create the application object or ensure that one already exists
661 // The app may have declared a global application object, but we recommend
662 // the IMPLEMENT_APP macro is used instead, which sets an initializer
663 // function for delayed, dynamic app object construction.
664 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
665 wxT("No initializer - use IMPLEMENT_APP macro.") );
667 wxTheApp
= (wxApp
*) (*wxApp::GetInitializerFunction()) ();
670 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
673 argc
= 0 ; // currently we don't support files as parameters
675 // we could try to get the open apple events here to adjust argc and argv better
677 wxTheApp
->argc
= argc
;
678 wxTheApp
->argv
= argv
;
680 // GUI-specific initialization, such as creating an app context.
684 // Here frames insert themselves automatically
685 // into wxTopLevelWindows by getting created
690 if ( wxTheApp
->OnInit() )
694 retValue
= wxTheApp
->OnRun();
697 // We want to initialize, but not run or exit immediately.
700 //else: app initialization failed, so we skipped OnRun()
702 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
705 // Forcibly delete the window.
706 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
707 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
709 topWindow
->Close(TRUE
);
710 wxTheApp
->DeletePendingObjects();
715 wxTheApp
->SetTopWindow(NULL
);
726 // Static member initialization
727 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
734 m_wantDebugOutput
= TRUE
;
739 m_printMode
= wxPRINT_WINDOWS
;
740 m_exitOnFrameDelete
= TRUE
;
744 bool wxApp::Initialized()
752 int wxApp::MainLoop()
764 // Returns TRUE if more time is needed.
765 bool wxApp::ProcessIdle()
768 event
.SetEventObject(this);
771 return event
.MoreRequested();
774 void wxApp::ExitMainLoop()
779 // Is a message/event pending?
780 bool wxApp::Pending()
784 return EventAvail( everyEvent
, &event
) ;
787 // Dispatch a message.
788 void wxApp::Dispatch()
793 void wxApp::OnIdle(wxIdleEvent
& event
)
795 static bool s_inOnIdle
= FALSE
;
797 // Avoid recursion (via ProcessEvent default case)
804 // 'Garbage' collection of windows deleted with Close().
805 DeletePendingObjects();
807 // flush the logged messages if any
808 wxLog
*pLog
= wxLog::GetActiveTarget();
809 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
812 // Send OnIdle events to all windows
813 bool needMore
= SendIdleEvents();
816 event
.RequestMore(TRUE
);
818 // If they are pending events, we must process them: pending events are
819 // either events to the threads other than main or events posted with
820 // wxPostEvent() functions
821 wxMacProcessNotifierAndPendingEvents();
831 // Send idle event to all top-level windows
832 bool wxApp::SendIdleEvents()
834 bool needMore
= FALSE
;
835 wxNode
* node
= wxTopLevelWindows
.First();
838 wxWindow
* win
= (wxWindow
*) node
->Data();
839 if (SendIdleEvents(win
))
847 // Send idle event to window and all subwindows
848 bool wxApp::SendIdleEvents(wxWindow
* win
)
850 bool needMore
= FALSE
;
853 event
.SetEventObject(win
);
854 win
->ProcessEvent(event
);
856 if (event
.MoreRequested())
859 wxNode
* node
= win
->GetChildren().First();
862 wxWindow
* win
= (wxWindow
*) node
->Data();
863 if (SendIdleEvents(win
))
871 void wxApp::DeletePendingObjects()
873 wxNode
*node
= wxPendingDelete
.First();
876 wxObject
*obj
= (wxObject
*)node
->Data();
880 if (wxPendingDelete
.Member(obj
))
883 // Deleting one object may have deleted other pending
884 // objects, so start from beginning of list again.
885 node
= wxPendingDelete
.First();
890 wxApp::GetStdIcon(int which
) const
894 case wxICON_INFORMATION
:
895 return wxIcon("wxICON_INFO");
897 case wxICON_QUESTION
:
898 return wxIcon("wxICON_QUESTION");
900 case wxICON_EXCLAMATION
:
901 return wxIcon("wxICON_WARNING");
904 wxFAIL_MSG(wxT("requested non existent standard icon"));
905 // still fall through
908 return wxIcon("wxICON_ERROR");
914 wxLogError(_("Fatal error: exiting"));
920 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
923 GetTopWindow()->Close(TRUE
);
926 // Default behaviour: close the application with prompts. The
927 // user can veto the close, and therefore the end session.
928 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
932 if (!GetTopWindow()->Close(!event
.CanVeto()))
937 extern "C" void wxCYield() ;
943 // Yield to other processes
945 bool wxApp::Yield(bool onlyIfNeeded
)
947 static bool s_inYield
= FALSE
;
953 wxFAIL_MSG( wxT("wxYield called recursively" ) );
966 long sleepTime
= 1 ; //::GetCaretTime();
968 while ( !wxTheApp
->IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, (RgnHandle
) wxApp::s_macCursorRgn
))
970 wxTheApp
->MacHandleOneEvent( &event
);
971 if ( event
.what
!= kHighLevelEvent
)
972 SetRectRgn( (RgnHandle
) wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
975 wxMacProcessNotifierAndPendingEvents() ;
982 // platform specifics
984 void wxApp::MacSuspend( bool convertClipboard
)
986 // we have to deactive the top level windows manually
988 wxNode
* node
= wxTopLevelWindows
.First();
991 wxTopLevelWindow
* win
= (wxTopLevelWindow
*) node
->Data();
992 win
->MacActivate( MacGetCurrentEvent() , false ) ;
997 s_lastMouseDown
= 0 ;
998 if( convertClipboard
)
1000 MacConvertPrivateToPublicScrap() ;
1003 ::HideFloatingWindows() ;
1006 void wxApp::MacResume( bool convertClipboard
)
1008 s_lastMouseDown
= 0 ;
1009 if( convertClipboard
)
1011 MacConvertPublicToPrivateScrap() ;
1014 ::ShowFloatingWindows() ;
1017 void wxApp::MacConvertPrivateToPublicScrap()
1021 void wxApp::MacConvertPublicToPrivateScrap()
1025 void wxApp::MacDoOneEvent()
1029 long sleepTime
= 1; // GetCaretTime() / 4 ;
1031 if (WaitNextEvent(everyEvent
, &event
, sleepTime
, (RgnHandle
) s_macCursorRgn
))
1033 MacHandleOneEvent( &event
);
1038 WindowPtr window
= ::FrontWindow() ;
1040 ::IdleControls( window
) ;
1042 wxTheApp
->ProcessIdle() ;
1044 if ( event
.what
!= kHighLevelEvent
)
1045 SetRectRgn( (RgnHandle
) s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1049 DeletePendingObjects() ;
1050 wxMacProcessNotifierAndPendingEvents() ;
1053 void wxApp::MacHandleOneEvent( WXEVENTREF evr
)
1055 EventRecord
* ev
= (EventRecord
*) evr
;
1056 m_macCurrentEvent
= ev
;
1058 wxApp::sm_lastMessageTime
= ev
->when
;
1063 MacHandleMouseDownEvent( ev
) ;
1064 if ( ev
->modifiers
& controlKey
)
1065 s_lastMouseDown
= 2;
1067 s_lastMouseDown
= 1;
1070 if ( s_lastMouseDown
== 2 )
1072 ev
->modifiers
|= controlKey
;
1076 ev
->modifiers
&= ~controlKey
;
1078 MacHandleMouseUpEvent( ev
) ;
1079 s_lastMouseDown
= 0;
1082 MacHandleActivateEvent( ev
) ;
1085 MacHandleUpdateEvent( ev
) ;
1089 MacHandleKeyDownEvent( ev
) ;
1092 MacHandleKeyUpEvent( ev
) ;
1095 MacHandleDiskEvent( ev
) ;
1098 MacHandleOSEvent( ev
) ;
1100 case kHighLevelEvent
:
1101 MacHandleHighLevelEvent( ev
) ;
1106 wxMacProcessNotifierAndPendingEvents() ;
1109 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr
)
1111 EventRecord
* ev
= (EventRecord
*) evr
;
1112 ::AEProcessAppleEvent( ev
) ;
1115 bool s_macIsInModalLoop
= false ;
1117 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr
)
1119 EventRecord
* ev
= (EventRecord
*) evr
;
1120 wxToolTip::RemoveToolTips() ;
1123 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1124 WindowAttributes frontWindowAttributes
= NULL
;
1126 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1128 short windowPart
= ::FindWindow(ev
->where
, &window
);
1129 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1130 if ( wxPendingDelete
.Member(win
) )
1134 GetQDGlobalsScreenBits( &screenBits
);
1139 if ( s_macIsInModalLoop
)
1145 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1146 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1147 s_lastMouseDown
= 0;
1152 SystemClick( ev
, window
) ;
1153 s_lastMouseDown
= 0;
1157 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1163 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1168 Point pt
= { 0, 0 } ;
1169 SetPortWindowPort(window
) ;
1170 LocalToGlobal( &pt
) ;
1172 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1173 -1 , wxSIZE_USE_EXISTING
);
1175 s_lastMouseDown
= 0;
1179 if (TrackGoAway(window
, ev
->where
))
1184 s_lastMouseDown
= 0;
1188 int growResult
= GrowWindow(window
, ev
->where
, &screenBits
.bounds
);
1189 if (growResult
!= 0)
1191 int newWidth
= LoWord(growResult
);
1192 int newHeight
= HiWord(growResult
);
1193 int oldWidth
, oldHeight
;
1198 win
->GetSize(&oldWidth
, &oldHeight
);
1200 newWidth
= oldWidth
;
1202 newHeight
= oldHeight
;
1203 win
->SetSize( -1, -1, newWidth
, newHeight
, wxSIZE_USE_EXISTING
);
1206 s_lastMouseDown
= 0;
1211 if (TrackBox(window
, ev
->where
, windowPart
))
1213 // TODO setup size event
1214 ZoomWindow( window
, windowPart
, false ) ;
1219 GetWindowPortBounds(window
, &tempRect
) ;
1220 win
->SetSize( -1, -1, tempRect
.right
-tempRect
.left
,
1221 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1224 s_lastMouseDown
= 0;
1226 case inCollapseBox
:
1227 // TODO setup size event
1228 s_lastMouseDown
= 0;
1235 SetPortWindowPort(window
) ;
1238 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1240 if ( s_macIsInModalLoop
)
1244 else if ( UMAIsWindowFloating( window
) )
1247 win
->MacMouseDown( ev
, windowPart
) ;
1252 win
->MacMouseDown( ev
, windowPart
) ;
1253 ::SelectWindow( window
) ;
1259 win
->MacMouseDown( ev
, windowPart
) ;
1268 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr
)
1270 EventRecord
* ev
= (EventRecord
*) evr
;
1273 short windowPart
= inNoWindow
;
1274 if ( wxTheApp
->s_captureWindow
)
1276 window
= (WindowRef
) s_captureWindow
->MacGetRootWindow() ;
1277 windowPart
= inContent
;
1281 windowPart
= ::FindWindow(ev
->where
, &window
) ;
1292 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1294 win
->MacMouseUp( ev
, windowPart
) ;
1300 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1301 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1310 retval
= WXK_RETURN
;
1325 retval
= WXK_PAGEUP
;
1328 retval
= WXK_PAGEDOWN
;
1331 retval
= WXK_RETURN
;
1386 retval
= WXK_ESCAPE
;
1392 retval
= WXK_RIGHT
;
1401 retval
= WXK_DELETE
;
1409 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1411 EventRecord
* ev
= (EventRecord
*) evr
;
1412 wxToolTip::RemoveToolTips() ;
1414 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1415 if ( HiWord( menuresult
) )
1417 if ( !s_macIsInModalLoop
)
1418 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1424 keychar
= short(ev
->message
& charCodeMask
);
1425 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1426 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1427 bool handled
= false ;
1428 wxWindow
* focus
= wxWindow::FindFocus() ;
1432 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1433 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1434 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1435 event
.m_altDown
= ev
->modifiers
& optionKey
;
1436 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1437 event
.m_keyCode
= keyval
;
1438 event
.m_x
= ev
->where
.h
;
1439 event
.m_y
= ev
->where
.v
;
1440 event
.m_timeStamp
= ev
->when
;
1441 event
.SetEventObject(focus
);
1442 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1448 wxWindow
*ancestor
= focus
;
1452 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1455 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1456 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1459 if (ancestor->m_isFrame)
1461 ancestor = ancestor->GetParent();
1465 #endif // wxUSE_ACCEL
1469 wxKeyEvent
event(wxEVT_CHAR
);
1470 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1471 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1472 event
.m_altDown
= ev
->modifiers
& optionKey
;
1473 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1474 event
.m_keyCode
= keyval
;
1475 event
.m_x
= ev
->where
.h
;
1476 event
.m_y
= ev
->where
.v
;
1477 event
.m_timeStamp
= ev
->when
;
1478 event
.SetEventObject(focus
);
1479 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1482 (keyval
== WXK_TAB
) &&
1483 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
1484 (focus
->GetParent()) &&
1485 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
1487 wxNavigationKeyEvent new_event
;
1488 new_event
.SetEventObject( focus
);
1489 new_event
.SetDirection( !event
.ShiftDown() );
1490 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1491 new_event
.SetWindowChange( event
.ControlDown() );
1492 new_event
.SetCurrentFocus( focus
);
1493 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1498 // if window is not having a focus still testing for default enter or cancel
1499 // TODO add the UMA version for ActiveNonFloatingWindow
1500 focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1503 if ( keyval
== WXK_RETURN
)
1505 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1507 if ( def
&& def
->IsEnabled() )
1509 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1510 event
.SetEventObject(def
);
1511 def
->Command(event
);
1515 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1516 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && ev
->modifiers
& cmdKey
) )
1518 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1519 new_event
.SetEventObject( focus
);
1520 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1527 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1529 EventRecord
* ev
= (EventRecord
*) evr
;
1530 wxToolTip::RemoveToolTips() ;
1532 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1533 if ( HiWord( menuresult
) )
1540 keychar
= short(ev
->message
& charCodeMask
);
1541 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1543 wxWindow
* focus
= wxWindow::FindFocus() ;
1546 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1548 wxKeyEvent
event(wxEVT_KEY_UP
);
1549 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1550 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1551 event
.m_altDown
= ev
->modifiers
& optionKey
;
1552 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1553 event
.m_keyCode
= keyval
;
1554 event
.m_x
= ev
->where
.h
;
1555 event
.m_y
= ev
->where
.v
;
1556 event
.m_timeStamp
= ev
->when
;
1557 event
.SetEventObject(focus
);
1558 bool handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1563 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
1565 EventRecord
* ev
= (EventRecord
*) evr
;
1566 WindowRef window
= (WindowRef
) ev
->message
;
1569 bool activate
= (ev
->modifiers
& activeFlag
) ;
1570 WindowClass wclass
;
1571 ::GetWindowClass ( window
, &wclass
) ;
1572 if ( wclass
== kFloatingWindowClass
)
1574 // if it is a floater we activate/deactivate the front non-floating window instead
1575 window
= ::FrontNonFloatingWindow() ;
1577 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1579 win
->MacActivate( ev
, activate
) ;
1583 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
1585 EventRecord
* ev
= (EventRecord
*) evr
;
1586 WindowRef window
= (WindowRef
) ev
->message
;
1587 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1590 if ( !wxPendingDelete
.Member(win
) )
1591 win
->MacUpdate( ev
->when
) ;
1595 // since there is no way of telling this foreign window to update itself
1596 // we have to invalidate the update region otherwise we keep getting the same
1597 // event over and over again
1598 BeginUpdate( window
) ;
1599 EndUpdate( window
) ;
1603 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
1605 EventRecord
* ev
= (EventRecord
*) evr
;
1606 if ( HiWord( ev
->message
) != noErr
)
1611 SetPt( &point
, 100 , 100 ) ;
1613 err
= DIBadMount( point
, ev
->message
) ;
1614 wxASSERT( err
== noErr
) ;
1619 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
1621 EventRecord
* ev
= (EventRecord
*) evr
;
1622 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
1624 case suspendResumeMessage
:
1626 bool isResuming
= ev
->message
& resumeFlag
;
1628 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
1630 bool convertClipboard
= false;
1632 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
1635 WindowRef oldFrontWindow
= NULL
;
1636 WindowRef newFrontWindow
= NULL
;
1638 // in case we don't take care of activating ourselves, we have to synchronize
1639 // our idea of the active window with the process manager's - which it already activated
1641 if ( !doesActivate
)
1642 oldFrontWindow
= ::FrontNonFloatingWindow() ;
1644 MacResume( convertClipboard
) ;
1646 newFrontWindow
= ::FrontNonFloatingWindow() ;
1648 if ( oldFrontWindow
)
1650 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
1652 win
->MacActivate( ev
, false ) ;
1654 if ( newFrontWindow
)
1656 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
1658 win
->MacActivate( ev
, true ) ;
1663 MacSuspend( convertClipboard
) ;
1665 // in case this suspending did close an active window, another one might
1666 // have surfaced -> lets deactivate that one
1668 /* TODO : find out what to do on systems < 10 , perhaps FrontNonFloatingWindow
1669 WindowRef newActiveWindow = ::ActiveNonFloatingWindow() ;
1670 if ( newActiveWindow )
1672 wxWindow* win = wxFindWinFromMacWindow( newActiveWindow ) ;
1674 win->MacActivate( ev , false ) ;
1680 case mouseMovedMessage
:
1684 wxWindow
* currentMouseWindow
= NULL
;
1686 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
1687 ¤tMouseWindow
) ;
1688 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
1690 wxMouseEvent event
;
1692 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
1693 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
1695 event
.m_leftDown
= isDown
&& !controlDown
;
1696 event
.m_middleDown
= FALSE
;
1697 event
.m_rightDown
= isDown
&& controlDown
;
1698 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1699 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1700 event
.m_altDown
= ev
->modifiers
& optionKey
;
1701 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1702 event
.m_x
= ev
->where
.h
;
1703 event
.m_y
= ev
->where
.v
;
1704 event
.m_timeStamp
= ev
->when
;
1705 event
.SetEventObject(this);
1707 if ( wxWindow::s_lastMouseWindow
)
1709 wxMouseEvent
eventleave(event
);
1710 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
);
1711 wxWindow::s_lastMouseWindow
->ScreenToClient( &eventleave
.m_x
, &eventleave
.m_y
);
1713 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
1715 if ( currentMouseWindow
)
1717 wxMouseEvent
evententer(event
);
1718 evententer
.SetEventType( wxEVT_ENTER_WINDOW
);
1719 currentMouseWindow
->ScreenToClient( &evententer
.m_x
, &evententer
.m_y
);
1721 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
1723 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
1726 short windowPart
= ::FindWindow(ev
->where
, &window
);
1730 // fixes for setting the cursor back from dominic mazzoni
1732 UMAShowArrowCursor();
1735 UMAShowArrowCursor();
1739 // if ( s_lastMouseDown == 0 )
1740 // ev->modifiers |= btnState ;
1742 // Calling GetNextEvent with a zero event mask will always
1743 // pass back a null event. However, it fills the EventRecord
1744 // with the state of the modifier keys. This is needed since
1745 // the modifier state returned by WaitForNextEvent often is
1746 // wrong mouse move events. The attempt above to correct this
1747 // didn't always work (under OS X at least).
1750 ::GetNextEvent(0, &tmp
);
1751 ev
->modifiers
= tmp
.modifiers
;
1753 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1755 win
->MacMouseMoved( ev
, windowPart
) ;
1757 UMAShowArrowCursor();
1768 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
1771 return; // no menu item selected
1773 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
1776 Str255 deskAccessoryName
;
1779 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
1780 GetPort(&savedPort
);
1781 OpenDeskAcc(deskAccessoryName
);
1787 wxWindow
* frontwindow
= wxFindWinFromMacWindow( ::FrontWindow() ) ;
1788 if ( frontwindow
&& wxMenuBar::MacGetInstalledMenuBar() )
1789 wxMenuBar::MacGetInstalledMenuBar()->MacMenuSelect( frontwindow
->GetEventHandler() , 0 , macMenuId
, macMenuItemNum
) ;
1795 long wxApp::MacTranslateKey(char key, int mods)
1799 void wxApp::MacAdjustCursor()
1806 wxApp::macAdjustCursor()
1808 if (ev->what != kHighLevelEvent)
1810 wxWindow* theMacWxFrame = wxFrame::MacFindFrameOrDialog(::FrontWindow());
1813 if (!theMacWxFrame->MacAdjustCursor(ev->where))
1814 ::SetCursor(&(qd.arrow));