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
= ::FindWindow(ev
->where
, &window
);
1283 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1285 win
->MacMouseUp( ev
, windowPart
) ;
1291 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1292 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1301 retval
= WXK_RETURN
;
1316 retval
= WXK_PAGEUP
;
1319 retval
= WXK_PAGEDOWN
;
1322 retval
= WXK_RETURN
;
1377 retval
= WXK_ESCAPE
;
1383 retval
= WXK_RIGHT
;
1392 retval
= WXK_DELETE
;
1400 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr
)
1402 EventRecord
* ev
= (EventRecord
*) evr
;
1403 wxToolTip::RemoveToolTips() ;
1405 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1406 if ( HiWord( menuresult
) )
1408 if ( !s_macIsInModalLoop
)
1409 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1415 keychar
= short(ev
->message
& charCodeMask
);
1416 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1417 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1418 bool handled
= false ;
1419 wxWindow
* focus
= wxWindow::FindFocus() ;
1423 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1424 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1425 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1426 event
.m_altDown
= ev
->modifiers
& optionKey
;
1427 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1428 event
.m_keyCode
= keyval
;
1429 event
.m_x
= ev
->where
.h
;
1430 event
.m_y
= ev
->where
.v
;
1431 event
.m_timeStamp
= ev
->when
;
1432 event
.SetEventObject(focus
);
1433 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1439 wxWindow
*ancestor
= focus
;
1443 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1446 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1447 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1450 if (ancestor->m_isFrame)
1452 ancestor = ancestor->GetParent();
1456 #endif // wxUSE_ACCEL
1460 wxKeyEvent
event(wxEVT_CHAR
);
1461 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1462 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1463 event
.m_altDown
= ev
->modifiers
& optionKey
;
1464 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1465 event
.m_keyCode
= keyval
;
1466 event
.m_x
= ev
->where
.h
;
1467 event
.m_y
= ev
->where
.v
;
1468 event
.m_timeStamp
= ev
->when
;
1469 event
.SetEventObject(focus
);
1470 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1473 (keyval
== WXK_TAB
) &&
1474 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
1475 (focus
->GetParent()) &&
1476 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
1478 wxNavigationKeyEvent new_event
;
1479 new_event
.SetEventObject( focus
);
1480 new_event
.SetDirection( !event
.ShiftDown() );
1481 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1482 new_event
.SetWindowChange( event
.ControlDown() );
1483 new_event
.SetCurrentFocus( focus
);
1484 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1489 // if window is not having a focus still testing for default enter or cancel
1490 // TODO add the UMA version for ActiveNonFloatingWindow
1491 focus
= wxFindWinFromMacWindow( FrontWindow() ) ;
1494 if ( keyval
== WXK_RETURN
)
1496 wxButton
*def
= wxDynamicCast(focus
->GetDefaultItem(),
1498 if ( def
&& def
->IsEnabled() )
1500 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1501 event
.SetEventObject(def
);
1502 def
->Command(event
);
1506 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1507 else if (keyval
== WXK_ESCAPE
|| (keyval
== '.' && ev
->modifiers
& cmdKey
) )
1509 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1510 new_event
.SetEventObject( focus
);
1511 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1518 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr
)
1520 EventRecord
* ev
= (EventRecord
*) evr
;
1521 wxToolTip::RemoveToolTips() ;
1523 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1524 if ( HiWord( menuresult
) )
1531 keychar
= short(ev
->message
& charCodeMask
);
1532 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1534 wxWindow
* focus
= wxWindow::FindFocus() ;
1537 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1539 wxKeyEvent
event(wxEVT_KEY_UP
);
1540 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1541 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1542 event
.m_altDown
= ev
->modifiers
& optionKey
;
1543 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1544 event
.m_keyCode
= keyval
;
1545 event
.m_x
= ev
->where
.h
;
1546 event
.m_y
= ev
->where
.v
;
1547 event
.m_timeStamp
= ev
->when
;
1548 event
.SetEventObject(focus
);
1549 bool handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1554 void wxApp::MacHandleActivateEvent( WXEVENTREF evr
)
1556 EventRecord
* ev
= (EventRecord
*) evr
;
1557 WindowRef window
= (WindowRef
) ev
->message
;
1560 bool activate
= (ev
->modifiers
& activeFlag
) ;
1561 WindowClass wclass
;
1562 ::GetWindowClass ( window
, &wclass
) ;
1563 if ( wclass
== kFloatingWindowClass
)
1565 // if it is a floater we activate/deactivate the front non-floating window instead
1566 window
= ::FrontNonFloatingWindow() ;
1568 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1570 win
->MacActivate( ev
, activate
) ;
1574 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr
)
1576 EventRecord
* ev
= (EventRecord
*) evr
;
1577 WindowRef window
= (WindowRef
) ev
->message
;
1578 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1581 if ( !wxPendingDelete
.Member(win
) )
1582 win
->MacUpdate( ev
->when
) ;
1586 // since there is no way of telling this foreign window to update itself
1587 // we have to invalidate the update region otherwise we keep getting the same
1588 // event over and over again
1589 BeginUpdate( window
) ;
1590 EndUpdate( window
) ;
1594 void wxApp::MacHandleDiskEvent( WXEVENTREF evr
)
1596 EventRecord
* ev
= (EventRecord
*) evr
;
1597 if ( HiWord( ev
->message
) != noErr
)
1602 SetPt( &point
, 100 , 100 ) ;
1604 err
= DIBadMount( point
, ev
->message
) ;
1605 wxASSERT( err
== noErr
) ;
1610 void wxApp::MacHandleOSEvent( WXEVENTREF evr
)
1612 EventRecord
* ev
= (EventRecord
*) evr
;
1613 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
1615 case suspendResumeMessage
:
1617 bool isResuming
= ev
->message
& resumeFlag
;
1619 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
1621 bool convertClipboard
= false;
1623 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
1626 WindowRef oldFrontWindow
= NULL
;
1627 WindowRef newFrontWindow
= NULL
;
1629 // in case we don't take care of activating ourselves, we have to synchronize
1630 // our idea of the active window with the process manager's - which it already activated
1632 if ( !doesActivate
)
1633 oldFrontWindow
= ::FrontNonFloatingWindow() ;
1635 MacResume( convertClipboard
) ;
1637 newFrontWindow
= ::FrontNonFloatingWindow() ;
1639 if ( oldFrontWindow
)
1641 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
1643 win
->MacActivate( ev
, false ) ;
1645 if ( newFrontWindow
)
1647 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
1649 win
->MacActivate( ev
, true ) ;
1654 MacSuspend( convertClipboard
) ;
1656 // in case this suspending did close an active window, another one might
1657 // have surfaced -> lets deactivate that one
1659 /* TODO : find out what to do on systems < 10 , perhaps FrontNonFloatingWindow
1660 WindowRef newActiveWindow = ::ActiveNonFloatingWindow() ;
1661 if ( newActiveWindow )
1663 wxWindow* win = wxFindWinFromMacWindow( newActiveWindow ) ;
1665 win->MacActivate( ev , false ) ;
1671 case mouseMovedMessage
:
1675 wxWindow
* currentMouseWindow
= NULL
;
1677 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
1678 ¤tMouseWindow
) ;
1680 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
1682 wxMouseEvent event
;
1684 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
1685 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
1687 event
.m_leftDown
= isDown
&& !controlDown
;
1688 event
.m_middleDown
= FALSE
;
1689 event
.m_rightDown
= isDown
&& controlDown
;
1690 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1691 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1692 event
.m_altDown
= ev
->modifiers
& optionKey
;
1693 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1694 event
.m_x
= ev
->where
.h
;
1695 event
.m_y
= ev
->where
.v
;
1696 event
.m_timeStamp
= ev
->when
;
1697 event
.SetEventObject(this);
1699 if ( wxWindow::s_lastMouseWindow
)
1701 wxMouseEvent
eventleave(event
) ;
1702 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
) ;
1703 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
1705 if ( currentMouseWindow
)
1707 wxMouseEvent
evententer(event
) ;
1708 evententer
.SetEventType( wxEVT_ENTER_WINDOW
) ;
1709 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
1711 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
1714 short windowPart
= ::FindWindow(ev
->where
, &window
);
1718 // fixes for setting the cursor back from dominic mazzoni
1720 UMAShowArrowCursor();
1723 UMAShowArrowCursor();
1727 // if ( s_lastMouseDown == 0 )
1728 // ev->modifiers |= btnState ;
1730 // Calling GetNextEvent with a zero event mask will always
1731 // pass back a null event. However, it fills the EventRecord
1732 // with the state of the modifier keys. This is needed since
1733 // the modifier state returned by WaitForNextEvent often is
1734 // wrong mouse move events. The attempt above to correct this
1735 // didn't always work (under OS X at least).
1738 ::GetNextEvent(0, &tmp
);
1739 ev
->modifiers
= tmp
.modifiers
;
1741 wxTopLevelWindowMac
* win
= wxFindWinFromMacWindow( window
) ;
1743 win
->MacMouseMoved( ev
, windowPart
) ;
1745 UMAShowArrowCursor();
1756 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
1759 return; // no menu item selected
1761 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
1764 Str255 deskAccessoryName
;
1767 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
1768 GetPort(&savedPort
);
1769 OpenDeskAcc(deskAccessoryName
);
1775 wxWindow
* frontwindow
= wxFindWinFromMacWindow( ::FrontWindow() ) ;
1776 if ( frontwindow
&& wxMenuBar::MacGetInstalledMenuBar() )
1777 wxMenuBar::MacGetInstalledMenuBar()->MacMenuSelect( frontwindow
->GetEventHandler() , 0 , macMenuId
, macMenuItemNum
) ;
1783 long wxApp::MacTranslateKey(char key, int mods)
1787 void wxApp::MacAdjustCursor()
1794 wxApp::macAdjustCursor()
1796 if (ev->what != kHighLevelEvent)
1798 wxWindow* theMacWxFrame = wxFrame::MacFindFrameOrDialog(::FrontWindow());
1801 if (!theMacWxFrame->MacAdjustCursor(ev->where))
1802 ::SetCursor(&(qd.arrow));