1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "app.h"
16 #include "wx/window.h"
20 #include "wx/gdicmn.h"
23 #include "wx/cursor.h"
26 #include "wx/palette.h"
28 #include "wx/dialog.h"
29 #include "wx/msgdlg.h"
31 #include "wx/module.h"
32 #include "wx/memory.h"
33 #include "wx/tooltip.h"
34 #include "wx/textctrl.h"
36 #if wxUSE_WX_RESOURCES
37 #include "wx/resource.h"
52 #include "wx/mac/uma.h"
53 #include "wx/mac/macnotfy.h"
57 #include <CoreServices/CoreServices.h>
59 #include <OpenTransport.h>
60 #include <OpenTptInternet.h>
64 extern char *wxBuffer
;
65 extern wxList wxPendingDelete
;
66 extern wxList
*wxWinMacWindowList
;
67 extern wxList
*wxWinMacControlList
;
69 wxApp
*wxTheApp
= NULL
;
71 #if !USE_SHARED_LIBRARY
72 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
73 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
74 EVT_IDLE(wxApp::OnIdle
)
75 EVT_END_SESSION(wxApp::OnEndSession
)
76 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
81 const short kMacMinHeap
= (29 * 1024) ;
82 // platform specific static variables
84 const short kwxMacMenuBarResource
= 1 ;
85 const short kwxMacAppleMenuId
= 1 ;
87 RgnHandle
wxApp::s_macCursorRgn
= NULL
;
88 wxWindow
* wxApp::s_captureWindow
= NULL
;
89 int wxApp::s_lastMouseDown
= 0 ;
90 long wxApp::sm_lastMessageTime
= 0;
92 bool wxApp::s_macDefaultEncodingIsPC
= true ;
93 bool wxApp::s_macSupportPCMenuShortcuts
= true ;
94 long wxApp::s_macAboutMenuItemId
= wxID_ABOUT
;
95 wxString
wxApp::s_macHelpMenuTitleName
= "&Help" ;
97 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
98 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
)
100 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, unsigned long refcon
)
103 wxApp
* app
= (wxApp
*) refcon
;
104 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
107 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
108 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
)
110 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, unsigned long refcon
)
113 wxApp
* app
= (wxApp
*) refcon
;
114 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
117 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
118 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
)
120 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, unsigned long refcon
)
123 wxApp
* app
= (wxApp
*) refcon
;
124 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
127 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
128 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
)
130 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, unsigned long refcon
)
133 wxApp
* app
= (wxApp
*) refcon
;
134 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
137 OSErr
wxApp::MacHandleAEODoc(const AppleEvent
*event
, AppleEvent
*reply
)
140 ProcessSerialNumber PSN
;
141 PSN
.highLongOfPSN
= 0 ;
142 PSN
.lowLongOfPSN
= kCurrentProcess
;
143 SetFrontProcess( &PSN
) ;
147 OSErr
wxApp::MacHandleAEPDoc(const AppleEvent
*event
, AppleEvent
*reply
)
152 OSErr
wxApp::MacHandleAEOApp(const AppleEvent
*event
, AppleEvent
*reply
)
157 OSErr
wxApp::MacHandleAEQuit(const AppleEvent
*event
, AppleEvent
*reply
)
159 wxWindow
* win
= GetTopWindow() ;
171 char StringMac
[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
172 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
173 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf"
174 "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf"
175 "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf"
176 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ;
178 char StringANSI
[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8"
179 "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC"
180 "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8"
181 "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8"
182 "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C"
183 "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ;
185 void wxMacConvertFromPC( const char *from
, char *to
, int len
)
190 for( int i
= 0 ; i
< len
; ++ i
)
192 c
= strchr( StringANSI
, *from
) ;
195 *to
= StringMac
[ c
- StringANSI
] ;
203 for( int i
= 0 ; i
< len
; ++ i
)
205 c
= strchr( StringANSI
, *from
) ;
208 *to
= StringMac
[ c
- StringANSI
] ;
220 void wxMacConvertToPC( const char *from
, char *to
, int len
)
225 for( int i
= 0 ; i
< len
; ++ i
)
227 c
= strchr( StringMac
, *from
) ;
230 *to
= StringANSI
[ c
- StringMac
] ;
238 for( int i
= 0 ; i
< len
; ++ i
)
240 c
= strchr( StringMac
, *from
) ;
243 *to
= StringANSI
[ c
- StringMac
] ;
255 void wxMacConvertFromPC( char * p
)
258 int len
= strlen ( p
) ;
260 wxMacConvertFromPC( ptr
, ptr
, len
) ;
263 void wxMacConvertFromPCForControls( char * p
)
266 int len
= strlen ( p
) ;
268 wxMacConvertFromPC( ptr
, ptr
, len
) ;
269 for ( int i
= 0 ; i
< strlen ( ptr
) ; i
++ )
271 if ( ptr
[i
] == '&' && ptr
[i
]+1 != ' ' )
273 memmove( &ptr
[i
] , &ptr
[i
+1] , strlen( &ptr
[i
+1] ) + 1) ;
278 void wxMacConvertFromPC( unsigned char *p
)
280 char *ptr
= (char*) p
+ 1 ;
283 wxMacConvertFromPC( ptr
, ptr
, len
) ;
286 extern char *wxBuffer
;
288 wxString
wxMacMakeMacStringFromPC( const char * p
)
290 const char *ptr
= p
;
291 int len
= strlen ( p
) ;
292 char *buf
= wxBuffer
;
294 if ( len
>= BUFSIZ
+ 512 )
296 buf
= new char [len
+1] ;
299 wxMacConvertFromPC( ptr
, buf
, len
) ;
301 wxString
result( buf
) ;
302 if ( buf
!= wxBuffer
)
308 void wxMacConvertToPC( char * p
)
311 int len
= strlen ( p
) ;
313 wxMacConvertToPC( ptr
, ptr
, len
) ;
316 void wxMacConvertToPC( unsigned char *p
)
318 char *ptr
= (char*) p
+ 1 ;
321 wxMacConvertToPC( ptr
, ptr
, len
) ;
324 wxString
wxMacMakePCStringFromMac( const char * p
)
326 const char *ptr
= p
;
327 int len
= strlen ( p
) ;
328 char *buf
= wxBuffer
;
330 if ( len
>= BUFSIZ
+ 512 )
332 buf
= new char [len
+1] ;
335 wxMacConvertToPC( ptr
, buf
, len
) ;
338 wxString
result( buf
) ;
339 if ( buf
!= wxBuffer
)
344 wxString
wxMacMakeStringFromMacString( const char* from
, bool mac2pcEncoding
)
348 return wxMacMakePCStringFromMac( from
) ;
352 return wxString( from
) ;
356 wxString
wxMacMakeStringFromPascal( StringPtr from
, bool mac2pcEncoding
)
358 // this is safe since a pascal string can never be larger than 256 bytes
360 CopyPascalStringToC( from
, s
) ;
363 return wxMacMakePCStringFromMac( s
) ;
367 return wxString( s
) ;
371 void wxMacStringToPascal( const char * from
, StringPtr to
, bool pc2macEncoding
)
375 CopyCStringToPascal( wxMacMakeMacStringFromPC( from
) , to
) ;
379 CopyCStringToPascal( from
, to
) ;
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
< 0x0750 )
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
460 GetIndString(message
, 128, error
);
461 UMAShowArrowCursor() ;
462 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
463 itemHit
= Alert(128, nil
);
468 #if __option(profile)
469 ProfilerInit( collectDetailed
, bestTimeBase
, 20000 , 40 ) ;
473 // now avoid exceptions thrown for new (bad_alloc)
476 std::__throws_bad_alloc
= FALSE
;
479 s_macCursorRgn
= ::NewRgn() ;
482 wxBuffer
= new char[1500];
484 wxBuffer
= new char[BUFSIZ
+ 512];
487 wxClassInfo::InitializeClasses();
490 // wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
494 wxPendingEventsLocker
= new wxCriticalSection
;
496 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
497 wxTheColourDatabase
->Initialize();
501 // flush the logged messages if any and install a 'safer' log target: the
502 // default one (wxLogGui) can't be used after the resources are freed just
503 // below and the user suppliedo ne might be even more unsafe (using any
504 // wxWindows GUI function is unsafe starting from now)
505 wxLog::DontCreateOnDemand();
507 // this will flush the old messages if any
508 delete wxLog::SetActiveTarget(new wxLogStderr
);
512 wxInitializeStockLists();
513 wxInitializeStockObjects();
515 #if wxUSE_WX_RESOURCES
516 wxInitializeResourceSystem();
519 wxBitmap::InitStandardHandlers();
521 wxModule::RegisterModules();
522 if (!wxModule::InitializeModules()) {
526 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
527 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
529 wxMacCreateNotifierTable() ;
531 UMAShowArrowCursor() ;
536 void wxApp::CleanUp()
539 // flush the logged messages if any and install a 'safer' log target: the
540 // default one (wxLogGui) can't be used after the resources are freed just
541 // below and the user suppliedo ne might be even more unsafe (using any
542 // wxWindows GUI function is unsafe starting from now)
543 wxLog::DontCreateOnDemand();
545 // this will flush the old messages if any
546 delete wxLog::SetActiveTarget(new wxLogStderr
);
549 // One last chance for pending objects to be cleaned up
550 wxTheApp
->DeletePendingObjects();
552 wxModule::CleanUpModules();
554 #if wxUSE_WX_RESOURCES
555 wxCleanUpResourceSystem();
558 wxDeleteStockObjects() ;
560 // Destroy all GDI lists, etc.
561 wxDeleteStockLists();
563 delete wxTheColourDatabase
;
564 wxTheColourDatabase
= NULL
;
566 wxBitmap::CleanUpHandlers();
571 wxMacDestroyNotifierTable() ;
572 if (wxWinMacWindowList
)
573 delete wxWinMacWindowList
;
575 delete wxPendingEvents
;
577 delete wxPendingEventsLocker
;
578 // If we don't do the following, we get an apparent memory leak.
579 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
582 wxClassInfo::CleanUpClasses();
585 #if __option(profile)
586 ProfilerDump( "\papp.prof" ) ;
594 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
595 // At this point we want to check if there are any memory
596 // blocks that aren't part of the wxDebugContext itself,
597 // as a special case. Then when dumping we need to ignore
598 // wxDebugContext, too.
599 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
601 wxLogDebug(wxT("There were memory leaks."));
602 wxDebugContext::Dump();
603 wxDebugContext::PrintStatistics();
605 // wxDebugContext::SetStream(NULL, NULL);
609 // do it as the very last thing because everything else can log messages
610 delete wxLog::SetActiveTarget(NULL
);
613 UMACleanupToolbox() ;
615 ::DisposeRgn(s_macCursorRgn
);
622 int wxEntry( int argc
, char *argv
[] , bool enterLoop
)
625 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
626 // This seems to be necessary since there are 'rogue'
627 // objects present at this point (perhaps global objects?)
628 // Setting a checkpoint will ignore them as far as the
629 // memory checking facility is concerned.
630 // Of course you may argue that memory allocated in globals should be
631 // checked, but this is a reasonable compromise.
632 wxDebugContext::SetCheckpoint();
635 if (!wxApp::Initialize()) {
638 // create the application object or ensure that one already exists
641 // The app may have declared a global application object, but we recommend
642 // the IMPLEMENT_APP macro is used instead, which sets an initializer
643 // function for delayed, dynamic app object construction.
644 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
645 wxT("No initializer - use IMPLEMENT_APP macro.") );
647 wxTheApp
= (wxApp
*) (*wxApp::GetInitializerFunction()) ();
650 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
653 argc
= 0 ; // currently we don't support files as parameters
656 wxTheApp
->argc
= argc
;
657 wxTheApp
->argv
= argv
;
659 // GUI-specific initialization, such as creating an app context.
660 wxTheApp
->OnInitGui();
662 // we could try to get the open apple events here to adjust argc and argv better
665 // Here frames insert themselves automatically
666 // into wxTopLevelWindows by getting created
671 if ( wxTheApp
->OnInit() )
675 retValue
= wxTheApp
->OnRun();
678 // We want to initialize, but not run or exit immediately.
681 //else: app initialization failed, so we skipped OnRun()
683 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
686 // Forcibly delete the window.
687 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
688 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
690 topWindow
->Close(TRUE
);
691 wxTheApp
->DeletePendingObjects();
696 wxTheApp
->SetTopWindow(NULL
);
707 // Static member initialization
708 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
715 m_wantDebugOutput
= TRUE
;
720 m_printMode
= wxPRINT_WINDOWS
;
721 m_exitOnFrameDelete
= TRUE
;
725 bool wxApp::Initialized()
733 int wxApp::MainLoop()
745 // Returns TRUE if more time is needed.
746 bool wxApp::ProcessIdle()
749 event
.SetEventObject(this);
752 return event
.MoreRequested();
755 void wxApp::ExitMainLoop()
760 // Is a message/event pending?
761 bool wxApp::Pending()
765 return EventAvail( everyEvent
, &event
) ;
768 // Dispatch a message.
769 void wxApp::Dispatch()
774 void wxApp::OnIdle(wxIdleEvent
& event
)
776 static bool s_inOnIdle
= FALSE
;
778 // Avoid recursion (via ProcessEvent default case)
785 // 'Garbage' collection of windows deleted with Close().
786 DeletePendingObjects();
788 // flush the logged messages if any
789 wxLog
*pLog
= wxLog::GetActiveTarget();
790 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
793 // Send OnIdle events to all windows
794 bool needMore
= SendIdleEvents();
797 event
.RequestMore(TRUE
);
799 // If they are pending events, we must process them: pending events are
800 // either events to the threads other than main or events posted with
801 // wxPostEvent() functions
802 wxMacProcessNotifierAndPendingEvents();
812 // Send idle event to all top-level windows
813 bool wxApp::SendIdleEvents()
815 bool needMore
= FALSE
;
816 wxNode
* node
= wxTopLevelWindows
.First();
819 wxWindow
* win
= (wxWindow
*) node
->Data();
820 if (SendIdleEvents(win
))
828 // Send idle event to window and all subwindows
829 bool wxApp::SendIdleEvents(wxWindow
* win
)
831 bool needMore
= FALSE
;
834 event
.SetEventObject(win
);
835 win
->ProcessEvent(event
);
837 if (event
.MoreRequested())
840 wxNode
* node
= win
->GetChildren().First();
843 wxWindow
* win
= (wxWindow
*) node
->Data();
844 if (SendIdleEvents(win
))
852 void wxApp::DeletePendingObjects()
854 wxNode
*node
= wxPendingDelete
.First();
857 wxObject
*obj
= (wxObject
*)node
->Data();
861 if (wxPendingDelete
.Member(obj
))
864 // Deleting one object may have deleted other pending
865 // objects, so start from beginning of list again.
866 node
= wxPendingDelete
.First();
871 wxApp::GetStdIcon(int which
) const
875 case wxICON_INFORMATION
:
876 return wxIcon("wxICON_INFO");
878 case wxICON_QUESTION
:
879 return wxIcon("wxICON_QUESTION");
881 case wxICON_EXCLAMATION
:
882 return wxIcon("wxICON_WARNING");
885 wxFAIL_MSG(wxT("requested non existent standard icon"));
886 // still fall through
889 return wxIcon("wxICON_ERROR");
895 wxLogError(_("Fatal error: exiting"));
901 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
904 GetTopWindow()->Close(TRUE
);
907 // Default behaviour: close the application with prompts. The
908 // user can veto the close, and therefore the end session.
909 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
913 if (!GetTopWindow()->Close(!event
.CanVeto()))
918 extern "C" void wxCYield() ;
924 // Yield to other processes
926 bool wxApp::Yield(bool onlyIfNeeded
)
928 static bool s_inYield
= FALSE
;
934 wxFAIL_MSG( wxT("wxYield called recursively" ) );
947 long sleepTime
= 1 ; //::GetCaretTime();
949 while ( !wxTheApp
->IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, wxApp::s_macCursorRgn
))
951 wxTheApp
->MacHandleOneEvent( &event
);
952 if ( event
.what
!= kHighLevelEvent
)
953 SetRectRgn( wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
956 wxMacProcessNotifierAndPendingEvents() ;
963 // platform specifics
965 void wxApp::MacSuspend( bool convertClipboard
)
967 // we have to deactive the window manually
969 wxWindow
* window
= GetTopWindow() ;
971 window
->MacActivate( MacGetCurrentEvent() , false ) ;
973 s_lastMouseDown
= 0 ;
974 if( convertClipboard
)
976 MacConvertPrivateToPublicScrap() ;
979 UMAHideFloatingWindows() ;
982 void wxApp::MacResume( bool convertClipboard
)
984 s_lastMouseDown
= 0 ;
985 if( convertClipboard
)
987 MacConvertPublicToPrivateScrap() ;
990 UMAShowFloatingWindows() ;
993 void wxApp::MacConvertPrivateToPublicScrap()
997 void wxApp::MacConvertPublicToPrivateScrap()
1001 void wxApp::MacDoOneEvent()
1005 long sleepTime
= 1 ; // GetCaretTime() / 4 ;
1007 if (WaitNextEvent(everyEvent
, &event
,sleepTime
, s_macCursorRgn
))
1009 MacHandleOneEvent( &event
);
1014 WindowPtr window
= UMAFrontWindow() ;
1016 UMAIdleControls( window
) ;
1018 wxTheApp
->ProcessIdle() ;
1020 if ( event
.what
!= kHighLevelEvent
)
1021 SetRectRgn( s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1025 DeletePendingObjects() ;
1026 wxMacProcessNotifierAndPendingEvents() ;
1029 void wxApp::MacHandleOneEvent( EventRecord
*ev
)
1031 m_macCurrentEvent
= ev
;
1033 wxApp::sm_lastMessageTime
= ev
->when
;
1038 MacHandleMouseDownEvent( ev
) ;
1039 if ( ev
->modifiers
& controlKey
)
1040 s_lastMouseDown
= 2;
1042 s_lastMouseDown
= 1;
1045 if ( s_lastMouseDown
== 2 )
1047 ev
->modifiers
|= controlKey
;
1051 ev
->modifiers
&= ~controlKey
;
1053 MacHandleMouseUpEvent( ev
) ;
1054 s_lastMouseDown
= 0;
1057 MacHandleActivateEvent( ev
) ;
1060 MacHandleUpdateEvent( ev
) ;
1064 MacHandleKeyDownEvent( ev
) ;
1067 MacHandleKeyUpEvent( ev
) ;
1070 MacHandleDiskEvent( ev
) ;
1073 MacHandleOSEvent( ev
) ;
1075 case kHighLevelEvent
:
1076 MacHandleHighLevelEvent( ev
) ;
1081 wxMacProcessNotifierAndPendingEvents() ;
1084 void wxApp::MacHandleHighLevelEvent( EventRecord
*ev
)
1086 ::AEProcessAppleEvent( ev
) ;
1089 bool s_macIsInModalLoop
= false ;
1091 void wxApp::MacHandleMouseDownEvent( EventRecord
*ev
)
1093 wxToolTip::RemoveToolTips() ;
1096 WindowRef frontWindow
= UMAFrontNonFloatingWindow() ;
1097 WindowAttributes frontWindowAttributes
= NULL
;
1099 UMAGetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1101 short windowPart
= ::FindWindow(ev
->where
, &window
);
1102 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
1103 if ( wxPendingDelete
.Member(win
) )
1107 GetQDGlobalsScreenBits( &screenBits
);
1112 if ( s_macIsInModalLoop
)
1118 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1119 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1120 s_lastMouseDown
= 0;
1125 SystemClick( ev
, window
) ;
1126 s_lastMouseDown
= 0;
1130 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1136 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1141 Point pt
= { 0, 0 } ;
1143 SetPort( GetWindowPort(window
) ) ;
1145 SetPort( (window
) ) ;
1147 SetOrigin( 0 , 0 ) ;
1148 LocalToGlobal( &pt
) ;
1150 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1151 -1 , wxSIZE_USE_EXISTING
);
1153 s_lastMouseDown
= 0;
1157 if (TrackGoAway(window
, ev
->where
))
1162 s_lastMouseDown
= 0;
1166 int growResult
= GrowWindow(window
, ev
->where
, &screenBits
.bounds
);
1167 if (growResult
!= 0)
1169 int newWidth
= LoWord(growResult
);
1170 int newHeight
= HiWord(growResult
);
1171 int oldWidth
, oldHeight
;
1176 win
->GetSize(&oldWidth
, &oldHeight
);
1178 newWidth
= oldWidth
;
1180 newHeight
= oldHeight
;
1181 win
->SetSize( -1, -1, newWidth
, newHeight
, wxSIZE_USE_EXISTING
);
1184 s_lastMouseDown
= 0;
1189 if (TrackBox(window
, ev
->where
, windowPart
))
1191 // TODO setup size event
1192 ZoomWindow( window
, windowPart
, false ) ;
1197 GetWindowPortBounds(window
, &tempRect
) ;
1198 win
->SetSize( -1, -1, tempRect
.right
-tempRect
.left
,
1199 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1202 s_lastMouseDown
= 0;
1204 case inCollapseBox
:
1205 // TODO setup size event
1206 s_lastMouseDown
= 0;
1214 SetPort( GetWindowPort(window
) ) ;
1216 SetPort( (window
) ) ;
1218 SetOrigin( 0 , 0 ) ;
1221 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1223 if ( s_macIsInModalLoop
)
1227 else if ( UMAIsWindowFloating( window
) )
1230 win
->MacMouseDown( ev
, windowPart
) ;
1235 win
->MacMouseDown( ev
, windowPart
) ;
1236 UMASelectWindow( window
) ;
1242 win
->MacMouseDown( ev
, windowPart
) ;
1251 void wxApp::MacHandleMouseUpEvent( EventRecord
*ev
)
1255 short windowPart
= ::FindWindow(ev
->where
, &window
);
1265 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
1267 win
->MacMouseUp( ev
, windowPart
) ;
1273 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1282 retval
= WXK_RETURN
;
1297 retval
= WXK_PAGEUP
;
1300 retval
= WXK_PAGEDOWN
;
1303 retval
= WXK_RETURN
;
1358 retval
= WXK_ESCAPE
;
1364 retval
= WXK_RIGHT
;
1373 retval
= WXK_DELETE
;
1381 void wxApp::MacHandleKeyDownEvent( EventRecord
*ev
)
1383 wxToolTip::RemoveToolTips() ;
1385 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1386 if ( HiWord( menuresult
) )
1388 if ( !s_macIsInModalLoop
)
1389 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1395 keychar
= short(ev
->message
& charCodeMask
);
1396 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1398 wxWindow
* focus
= wxWindow::FindFocus() ;
1401 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1403 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1404 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1405 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1406 event
.m_altDown
= ev
->modifiers
& optionKey
;
1407 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1408 event
.m_keyCode
= keyval
;
1409 event
.m_x
= ev
->where
.h
;
1410 event
.m_y
= ev
->where
.v
;
1411 event
.m_timeStamp
= ev
->when
;
1412 event
.SetEventObject(focus
);
1413 bool handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1419 wxWindow
*ancestor
= focus
;
1423 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1426 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1427 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1430 if (ancestor->m_isFrame)
1432 ancestor = ancestor->GetParent();
1436 #endif // wxUSE_ACCEL
1440 wxKeyEvent
event(wxEVT_CHAR
);
1441 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1442 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1443 event
.m_altDown
= ev
->modifiers
& optionKey
;
1444 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1445 event
.m_keyCode
= keyval
;
1446 event
.m_x
= ev
->where
.h
;
1447 event
.m_y
= ev
->where
.v
;
1448 event
.m_timeStamp
= ev
->when
;
1449 event
.SetEventObject(focus
);
1450 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1453 (keyval
== WXK_TAB
) &&
1454 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
1455 (focus
->GetParent()) &&
1456 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
1458 wxNavigationKeyEvent new_event
;
1459 new_event
.SetEventObject( focus
);
1460 new_event
.SetDirection( !event
.ShiftDown() );
1461 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1462 new_event
.SetWindowChange( event
.ControlDown() );
1463 new_event
.SetCurrentFocus( focus
);
1464 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1466 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1468 (keyval
== '.' && event
.ControlDown() ) )
1470 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1471 new_event
.SetEventObject( focus
);
1472 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1478 void wxApp::MacHandleKeyUpEvent( EventRecord
*ev
)
1480 wxToolTip::RemoveToolTips() ;
1482 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1483 if ( HiWord( menuresult
) )
1490 keychar
= short(ev
->message
& charCodeMask
);
1491 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1493 wxWindow
* focus
= wxWindow::FindFocus() ;
1496 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1498 wxKeyEvent
event(wxEVT_KEY_UP
);
1499 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1500 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1501 event
.m_altDown
= ev
->modifiers
& optionKey
;
1502 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1503 event
.m_keyCode
= keyval
;
1504 event
.m_x
= ev
->where
.h
;
1505 event
.m_y
= ev
->where
.v
;
1506 event
.m_timeStamp
= ev
->when
;
1507 event
.SetEventObject(focus
);
1508 bool handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1513 void wxApp::MacHandleActivateEvent( EventRecord
*ev
)
1515 WindowRef window
= (WindowRef
) ev
->message
;
1518 bool activate
= (ev
->modifiers
& activeFlag
) ;
1519 WindowClass wclass
;
1520 UMAGetWindowClass ( window
, &wclass
) ;
1521 if ( wclass
== kFloatingWindowClass
)
1523 // if it is a floater we activate/deactivate the front non-floating window instead
1524 window
= UMAFrontNonFloatingWindow() ;
1526 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
1528 win
->MacActivate( ev
, activate
) ;
1532 void wxApp::MacHandleUpdateEvent( EventRecord
*ev
)
1534 WindowRef window
= (WindowRef
) ev
->message
;
1535 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
1538 if ( !wxPendingDelete
.Member(win
) )
1539 win
->MacUpdate( ev
) ;
1543 // since there is no way of telling this foreign window to update itself
1544 // we have to invalidate the update region otherwise we keep getting the same
1545 // event over and over again
1546 BeginUpdate( window
) ;
1547 EndUpdate( window
) ;
1551 void wxApp::MacHandleDiskEvent( EventRecord
*ev
)
1553 if ( HiWord( ev
->message
) != noErr
)
1558 SetPt( &point
, 100 , 100 ) ;
1560 err
= DIBadMount( point
, ev
->message
) ;
1561 wxASSERT( err
== noErr
) ;
1566 void wxApp::MacHandleOSEvent( EventRecord
*ev
)
1568 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
1570 case suspendResumeMessage
:
1572 bool isResuming
= ev
->message
& resumeFlag
;
1574 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
1576 bool convertClipboard
= false;
1578 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
1581 WindowRef oldFrontWindow
= NULL
;
1582 WindowRef newFrontWindow
= NULL
;
1584 // in case we don't take care of activating ourselves, we have to synchronize
1585 // our idea of the active window with the process manager's - which it already activated
1587 if ( !doesActivate
)
1588 oldFrontWindow
= UMAFrontNonFloatingWindow() ;
1590 MacResume( convertClipboard
) ;
1592 newFrontWindow
= UMAFrontNonFloatingWindow() ;
1594 if ( oldFrontWindow
)
1596 wxWindow
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
1598 win
->MacActivate( ev
, false ) ;
1600 if ( newFrontWindow
)
1602 wxWindow
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
1604 win
->MacActivate( ev
, true ) ;
1609 MacSuspend( convertClipboard
) ;
1611 // in case this suspending did close an active window, another one might
1612 // have surfaced -> lets deactivate that one
1614 WindowRef newActiveWindow
= UMAGetActiveNonFloatingWindow() ;
1615 if ( newActiveWindow
)
1617 wxWindow
* win
= wxFindWinFromMacWindow( newActiveWindow
) ;
1619 win
->MacActivate( ev
, false ) ;
1624 case mouseMovedMessage
:
1628 wxWindow
* currentMouseWindow
= NULL
;
1630 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
1631 ¤tMouseWindow
) ;
1633 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
1635 wxMouseEvent event
;
1637 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
1638 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
1640 event
.m_leftDown
= isDown
&& !controlDown
;
1641 event
.m_middleDown
= FALSE
;
1642 event
.m_rightDown
= isDown
&& controlDown
;
1643 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1644 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1645 event
.m_altDown
= ev
->modifiers
& optionKey
;
1646 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1647 event
.m_x
= ev
->where
.h
;
1648 event
.m_y
= ev
->where
.v
;
1649 event
.m_timeStamp
= ev
->when
;
1650 event
.SetEventObject(this);
1652 if ( wxWindow::s_lastMouseWindow
)
1654 wxMouseEvent
eventleave(event
) ;
1655 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
) ;
1656 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
1658 if ( currentMouseWindow
)
1660 wxMouseEvent
evententer(event
) ;
1661 evententer
.SetEventType( wxEVT_ENTER_WINDOW
) ;
1662 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
1664 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
1667 short windowPart
= ::FindWindow(ev
->where
, &window
);
1671 // fixes for setting the cursor back from dominic mazzoni
1673 UMAShowArrowCursor();
1676 UMAShowArrowCursor();
1680 if ( s_lastMouseDown
== 0 )
1681 ev
->modifiers
|= btnState
;
1683 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
1685 win
->MacMouseMoved( ev
, windowPart
) ;
1687 UMAShowArrowCursor();
1698 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
1701 return; // no menu item selected
1703 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
1706 Str255 deskAccessoryName
;
1709 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
1710 GetPort(&savedPort
);
1711 OpenDeskAcc(deskAccessoryName
);
1717 wxWindow
* frontwindow
= wxFindWinFromMacWindow( ::FrontWindow() ) ;
1718 if ( frontwindow
&& wxMenuBar::MacGetInstalledMenuBar() )
1719 wxMenuBar::MacGetInstalledMenuBar()->MacMenuSelect( frontwindow
->GetEventHandler() , 0 , macMenuId
, macMenuItemNum
) ;
1725 long wxApp::MacTranslateKey(char key, int mods)
1729 void wxApp::MacAdjustCursor()
1736 wxApp::macAdjustCursor()
1738 if (ev->what != kHighLevelEvent)
1740 wxWindow* theMacWxFrame = wxFrame::MacFindFrameOrDialog(::FrontWindow());
1743 if (!theMacWxFrame->MacAdjustCursor(ev->where))
1744 ::SetCursor(&(qd.arrow));