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 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
98 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
99 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
100 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
) ;
103 pascal OSErr
AEHandleODoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
)
105 wxApp
* app
= (wxApp
*) refcon
;
106 return wxTheApp
->MacHandleAEODoc( (AppleEvent
*) event
, reply
) ;
109 pascal OSErr
AEHandleOApp( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
)
111 wxApp
* app
= (wxApp
*) refcon
;
112 return wxTheApp
->MacHandleAEOApp( (AppleEvent
*) event
, reply
) ;
115 pascal OSErr
AEHandlePDoc( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
)
117 wxApp
* app
= (wxApp
*) refcon
;
118 return wxTheApp
->MacHandleAEPDoc( (AppleEvent
*) event
, reply
) ;
121 pascal OSErr
AEHandleQuit( const AppleEvent
*event
, AppleEvent
*reply
, long refcon
)
123 wxApp
* app
= (wxApp
*) refcon
;
124 return wxTheApp
->MacHandleAEQuit( (AppleEvent
*) event
, reply
) ;
127 OSErr
wxApp::MacHandleAEODoc(const AppleEvent
*event
, AppleEvent
*reply
)
130 ProcessSerialNumber PSN
;
131 PSN
.highLongOfPSN
= 0 ;
132 PSN
.lowLongOfPSN
= kCurrentProcess
;
133 SetFrontProcess( &PSN
) ;
137 OSErr
wxApp::MacHandleAEPDoc(const AppleEvent
*event
, AppleEvent
*reply
)
142 OSErr
wxApp::MacHandleAEOApp(const AppleEvent
*event
, AppleEvent
*reply
)
147 OSErr
wxApp::MacHandleAEQuit(const AppleEvent
*event
, AppleEvent
*reply
)
149 wxWindow
* win
= GetTopWindow() ;
161 char StringMac
[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
162 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
163 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf"
164 "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf"
165 "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf"
166 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ;
168 char StringANSI
[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8"
169 "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC"
170 "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8"
171 "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8"
172 "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C"
173 "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ;
175 void wxMacConvertFromPC( const char *from
, char *to
, int len
)
180 for( int i
= 0 ; i
< len
; ++ i
)
182 c
= strchr( StringANSI
, *from
) ;
185 *to
= StringMac
[ c
- StringANSI
] ;
193 for( int i
= 0 ; i
< len
; ++ i
)
195 c
= strchr( StringANSI
, *from
) ;
198 *to
= StringMac
[ c
- StringANSI
] ;
210 void wxMacConvertToPC( const char *from
, char *to
, int len
)
215 for( int i
= 0 ; i
< len
; ++ i
)
217 c
= strchr( StringMac
, *from
) ;
220 *to
= StringANSI
[ c
- StringMac
] ;
228 for( int i
= 0 ; i
< len
; ++ i
)
230 c
= strchr( StringMac
, *from
) ;
233 *to
= StringANSI
[ c
- StringMac
] ;
245 void wxMacConvertFromPC( char * p
)
248 int len
= strlen ( p
) ;
250 wxMacConvertFromPC( ptr
, ptr
, len
) ;
253 void wxMacConvertFromPCForControls( char * p
)
256 int len
= strlen ( p
) ;
258 wxMacConvertFromPC( ptr
, ptr
, len
) ;
259 for ( int i
= 0 ; i
< strlen ( ptr
) ; i
++ )
261 if ( ptr
[i
] == '&' && ptr
[i
]+1 != ' ' )
263 memmove( &ptr
[i
] , &ptr
[i
+1] , strlen( &ptr
[i
+1] ) + 1) ;
268 void wxMacConvertFromPC( unsigned char *p
)
270 char *ptr
= (char*) p
+ 1 ;
273 wxMacConvertFromPC( ptr
, ptr
, len
) ;
276 extern char *wxBuffer
;
278 wxString
wxMacMakeMacStringFromPC( const char * p
)
280 const char *ptr
= p
;
281 int len
= strlen ( p
) ;
282 char *buf
= wxBuffer
;
284 if ( len
>= BUFSIZ
+ 512 )
286 buf
= new char [len
+1] ;
289 wxMacConvertFromPC( ptr
, buf
, len
) ;
291 wxString
result( buf
) ;
292 if ( buf
!= wxBuffer
)
298 void wxMacConvertToPC( char * p
)
301 int len
= strlen ( p
) ;
303 wxMacConvertToPC( ptr
, ptr
, len
) ;
306 void wxMacConvertToPC( unsigned char *p
)
308 char *ptr
= (char*) p
+ 1 ;
311 wxMacConvertToPC( ptr
, ptr
, len
) ;
314 wxString
wxMacMakePCStringFromMac( const char * p
)
316 const char *ptr
= p
;
317 int len
= strlen ( p
) ;
318 char *buf
= wxBuffer
;
320 if ( len
>= BUFSIZ
+ 512 )
322 buf
= new char [len
+1] ;
325 wxMacConvertToPC( ptr
, buf
, len
) ;
328 wxString
result( buf
) ;
329 if ( buf
!= wxBuffer
)
334 wxString
wxMacMakeStringFromMacString( const char* from
, bool mac2pcEncoding
)
338 return wxMacMakePCStringFromMac( from
) ;
342 return wxString( from
) ;
346 wxString
wxMacMakeStringFromPascal( StringPtr from
, bool mac2pcEncoding
)
348 // this is safe since a pascal string can never be larger than 256 bytes
350 CopyPascalStringToC( from
, s
) ;
353 return wxMacMakePCStringFromMac( s
) ;
357 return wxString( s
) ;
361 void wxMacStringToPascal( const char * from
, StringPtr to
, bool pc2macEncoding
)
365 CopyCStringToPascal( wxMacMakeMacStringFromPC( from
) , to
) ;
369 CopyCStringToPascal( from
, to
) ;
373 bool wxApp::Initialize()
379 UMAInitToolbox( 4 ) ;
380 SetEventMask( everyEvent
) ;
381 UMAShowWatchCursor() ;
383 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
384 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
, NewAEEventHandlerUPP(AEHandleODoc
) ,
385 (long) wxTheApp
, FALSE
) ;
386 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
, NewAEEventHandlerUPP(AEHandleOApp
) ,
387 (long) wxTheApp
, FALSE
) ;
388 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
, NewAEEventHandlerUPP(AEHandlePDoc
) ,
389 (long) wxTheApp
, FALSE
) ;
390 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
, NewAEEventHandlerUPP(AEHandleQuit
) ,
391 (long) wxTheApp
, FALSE
) ;
393 AEInstallEventHandler( kCoreEventClass
, kAEOpenDocuments
, NewAEEventHandlerProc(AEHandleODoc
) ,
394 (long) wxTheApp
, FALSE
) ;
395 AEInstallEventHandler( kCoreEventClass
, kAEOpenApplication
, NewAEEventHandlerProc(AEHandleOApp
) ,
396 (long) wxTheApp
, FALSE
) ;
397 AEInstallEventHandler( kCoreEventClass
, kAEPrintDocuments
, NewAEEventHandlerProc(AEHandlePDoc
) ,
398 (long) wxTheApp
, FALSE
) ;
399 AEInstallEventHandler( kCoreEventClass
, kAEQuitApplication
, NewAEEventHandlerProc(AEHandleQuit
) ,
400 (long) wxTheApp
, FALSE
) ;
405 // test the minimal configuration necessary
411 if (Gestalt(gestaltMachineType
, &theMachine
) != noErr
)
413 error
= kMacSTRWrongMachine
;
415 else if (theMachine
< gestaltMacPlus
)
417 error
= kMacSTRWrongMachine
;
419 else if (Gestalt(gestaltSystemVersion
, &theSystem
) != noErr
)
421 error
= kMacSTROldSystem
;
423 else if ( theSystem
< 0x0860 )
425 error
= kMacSTROldSystem
;
427 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap
)
429 error
= kMacSTRSmallSize
;
435 if ( !UMAHasAppearance() )
437 error = kMacSTRNoPre8Yet ;
443 // if we encountered any problems so far, give the error code and exit immediately
450 GetIndString(message
, 128, error
);
451 UMAShowArrowCursor() ;
452 ParamText("\pFatal Error", message
, (ConstStr255Param
)"\p", (ConstStr255Param
)"\p");
453 itemHit
= Alert(128, nil
);
458 #if __option(profile)
459 ProfilerInit( collectDetailed
, bestTimeBase
, 20000 , 40 ) ;
463 // now avoid exceptions thrown for new (bad_alloc)
466 std::__throws_bad_alloc
= FALSE
;
469 s_macCursorRgn
= ::NewRgn() ;
472 wxBuffer
= new char[1500];
474 wxBuffer
= new char[BUFSIZ
+ 512];
477 wxClassInfo::InitializeClasses();
480 // wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
484 wxPendingEventsLocker
= new wxCriticalSection
;
486 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
487 wxTheColourDatabase
->Initialize();
491 // flush the logged messages if any and install a 'safer' log target: the
492 // default one (wxLogGui) can't be used after the resources are freed just
493 // below and the user suppliedo ne might be even more unsafe (using any
494 // wxWindows GUI function is unsafe starting from now)
495 wxLog::DontCreateOnDemand();
497 // this will flush the old messages if any
498 delete wxLog::SetActiveTarget(new wxLogStderr
);
502 wxInitializeStockLists();
503 wxInitializeStockObjects();
505 #if wxUSE_WX_RESOURCES
506 wxInitializeResourceSystem();
509 wxBitmap::InitStandardHandlers();
511 wxModule::RegisterModules();
512 if (!wxModule::InitializeModules()) {
516 wxWinMacWindowList
= new wxList(wxKEY_INTEGER
);
517 wxWinMacControlList
= new wxList(wxKEY_INTEGER
);
519 wxMacCreateNotifierTable() ;
521 UMAShowArrowCursor() ;
526 void wxApp::CleanUp()
529 // flush the logged messages if any and install a 'safer' log target: the
530 // default one (wxLogGui) can't be used after the resources are freed just
531 // below and the user suppliedo ne might be even more unsafe (using any
532 // wxWindows GUI function is unsafe starting from now)
533 wxLog::DontCreateOnDemand();
535 // this will flush the old messages if any
536 delete wxLog::SetActiveTarget(new wxLogStderr
);
539 // One last chance for pending objects to be cleaned up
540 wxTheApp
->DeletePendingObjects();
542 wxModule::CleanUpModules();
544 #if wxUSE_WX_RESOURCES
545 wxCleanUpResourceSystem();
548 wxDeleteStockObjects() ;
550 // Destroy all GDI lists, etc.
551 wxDeleteStockLists();
553 delete wxTheColourDatabase
;
554 wxTheColourDatabase
= NULL
;
556 wxBitmap::CleanUpHandlers();
561 wxMacDestroyNotifierTable() ;
562 if (wxWinMacWindowList
)
563 delete wxWinMacWindowList
;
565 delete wxPendingEvents
;
567 delete wxPendingEventsLocker
;
568 // If we don't do the following, we get an apparent memory leak.
569 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
572 wxClassInfo::CleanUpClasses();
575 #if __option(profile)
576 ProfilerDump( "\papp.prof" ) ;
584 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
585 // At this point we want to check if there are any memory
586 // blocks that aren't part of the wxDebugContext itself,
587 // as a special case. Then when dumping we need to ignore
588 // wxDebugContext, too.
589 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
591 wxLogDebug(wxT("There were memory leaks."));
592 wxDebugContext::Dump();
593 wxDebugContext::PrintStatistics();
595 // wxDebugContext::SetStream(NULL, NULL);
599 // do it as the very last thing because everything else can log messages
600 delete wxLog::SetActiveTarget(NULL
);
603 UMACleanupToolbox() ;
605 ::DisposeRgn(s_macCursorRgn
);
612 //----------------------------------------------------------------------
614 //----------------------------------------------------------------------
616 int wxEntryStart( int argc
, char *argv
[] )
618 return wxApp::Initialize();
624 return wxTheApp
->OnInitGui();
628 void wxEntryCleanup()
634 int wxEntry( int argc
, char *argv
[] , bool enterLoop
)
637 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
638 // This seems to be necessary since there are 'rogue'
639 // objects present at this point (perhaps global objects?)
640 // Setting a checkpoint will ignore them as far as the
641 // memory checking facility is concerned.
642 // Of course you may argue that memory allocated in globals should be
643 // checked, but this is a reasonable compromise.
644 wxDebugContext::SetCheckpoint();
647 if (!wxEntryStart(argc
, argv
)) {
650 // create the application object or ensure that one already exists
653 // The app may have declared a global application object, but we recommend
654 // the IMPLEMENT_APP macro is used instead, which sets an initializer
655 // function for delayed, dynamic app object construction.
656 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
657 wxT("No initializer - use IMPLEMENT_APP macro.") );
659 wxTheApp
= (wxApp
*) (*wxApp::GetInitializerFunction()) ();
662 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
665 argc
= 0 ; // currently we don't support files as parameters
667 // we could try to get the open apple events here to adjust argc and argv better
669 wxTheApp
->argc
= argc
;
670 wxTheApp
->argv
= argv
;
672 // GUI-specific initialization, such as creating an app context.
676 // Here frames insert themselves automatically
677 // into wxTopLevelWindows by getting created
682 if ( wxTheApp
->OnInit() )
686 retValue
= wxTheApp
->OnRun();
689 // We want to initialize, but not run or exit immediately.
692 //else: app initialization failed, so we skipped OnRun()
694 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
697 // Forcibly delete the window.
698 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
699 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
701 topWindow
->Close(TRUE
);
702 wxTheApp
->DeletePendingObjects();
707 wxTheApp
->SetTopWindow(NULL
);
718 // Static member initialization
719 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
726 m_wantDebugOutput
= TRUE
;
731 m_printMode
= wxPRINT_WINDOWS
;
732 m_exitOnFrameDelete
= TRUE
;
736 bool wxApp::Initialized()
744 int wxApp::MainLoop()
756 // Returns TRUE if more time is needed.
757 bool wxApp::ProcessIdle()
760 event
.SetEventObject(this);
763 return event
.MoreRequested();
766 void wxApp::ExitMainLoop()
771 // Is a message/event pending?
772 bool wxApp::Pending()
776 return EventAvail( everyEvent
, &event
) ;
779 // Dispatch a message.
780 void wxApp::Dispatch()
785 void wxApp::OnIdle(wxIdleEvent
& event
)
787 static bool s_inOnIdle
= FALSE
;
789 // Avoid recursion (via ProcessEvent default case)
796 // 'Garbage' collection of windows deleted with Close().
797 DeletePendingObjects();
799 // flush the logged messages if any
800 wxLog
*pLog
= wxLog::GetActiveTarget();
801 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
804 // Send OnIdle events to all windows
805 bool needMore
= SendIdleEvents();
808 event
.RequestMore(TRUE
);
810 // If they are pending events, we must process them: pending events are
811 // either events to the threads other than main or events posted with
812 // wxPostEvent() functions
813 wxMacProcessNotifierAndPendingEvents();
823 // Send idle event to all top-level windows
824 bool wxApp::SendIdleEvents()
826 bool needMore
= FALSE
;
827 wxNode
* node
= wxTopLevelWindows
.First();
830 wxWindow
* win
= (wxWindow
*) node
->Data();
831 if (SendIdleEvents(win
))
839 // Send idle event to window and all subwindows
840 bool wxApp::SendIdleEvents(wxWindow
* win
)
842 bool needMore
= FALSE
;
845 event
.SetEventObject(win
);
846 win
->ProcessEvent(event
);
848 if (event
.MoreRequested())
851 wxNode
* node
= win
->GetChildren().First();
854 wxWindow
* win
= (wxWindow
*) node
->Data();
855 if (SendIdleEvents(win
))
863 void wxApp::DeletePendingObjects()
865 wxNode
*node
= wxPendingDelete
.First();
868 wxObject
*obj
= (wxObject
*)node
->Data();
872 if (wxPendingDelete
.Member(obj
))
875 // Deleting one object may have deleted other pending
876 // objects, so start from beginning of list again.
877 node
= wxPendingDelete
.First();
882 wxApp::GetStdIcon(int which
) const
886 case wxICON_INFORMATION
:
887 return wxIcon("wxICON_INFO");
889 case wxICON_QUESTION
:
890 return wxIcon("wxICON_QUESTION");
892 case wxICON_EXCLAMATION
:
893 return wxIcon("wxICON_WARNING");
896 wxFAIL_MSG(wxT("requested non existent standard icon"));
897 // still fall through
900 return wxIcon("wxICON_ERROR");
906 wxLogError(_("Fatal error: exiting"));
912 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
915 GetTopWindow()->Close(TRUE
);
918 // Default behaviour: close the application with prompts. The
919 // user can veto the close, and therefore the end session.
920 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
924 if (!GetTopWindow()->Close(!event
.CanVeto()))
929 extern "C" void wxCYield() ;
935 // Yield to other processes
937 bool wxApp::Yield(bool onlyIfNeeded
)
939 static bool s_inYield
= FALSE
;
945 wxFAIL_MSG( wxT("wxYield called recursively" ) );
958 long sleepTime
= 1 ; //::GetCaretTime();
960 while ( !wxTheApp
->IsExiting() && WaitNextEvent(everyEvent
, &event
,sleepTime
, wxApp::s_macCursorRgn
))
962 wxTheApp
->MacHandleOneEvent( &event
);
963 if ( event
.what
!= kHighLevelEvent
)
964 SetRectRgn( wxApp::s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
967 wxMacProcessNotifierAndPendingEvents() ;
974 // platform specifics
976 void wxApp::MacSuspend( bool convertClipboard
)
978 // we have to deactive the window manually
980 wxWindow
* window
= GetTopWindow() ;
982 window
->MacActivate( MacGetCurrentEvent() , false ) ;
984 s_lastMouseDown
= 0 ;
985 if( convertClipboard
)
987 MacConvertPrivateToPublicScrap() ;
990 ::HideFloatingWindows() ;
993 void wxApp::MacResume( bool convertClipboard
)
995 s_lastMouseDown
= 0 ;
996 if( convertClipboard
)
998 MacConvertPublicToPrivateScrap() ;
1001 ::ShowFloatingWindows() ;
1004 void wxApp::MacConvertPrivateToPublicScrap()
1008 void wxApp::MacConvertPublicToPrivateScrap()
1012 void wxApp::MacDoOneEvent()
1016 long sleepTime
= 1; // GetCaretTime() / 4 ;
1018 if (WaitNextEvent(everyEvent
, &event
, sleepTime
, s_macCursorRgn
))
1020 MacHandleOneEvent( &event
);
1025 WindowPtr window
= ::FrontWindow() ;
1027 ::IdleControls( window
) ;
1029 wxTheApp
->ProcessIdle() ;
1031 if ( event
.what
!= kHighLevelEvent
)
1032 SetRectRgn( s_macCursorRgn
, event
.where
.h
, event
.where
.v
, event
.where
.h
+ 1 , event
.where
.v
+ 1 ) ;
1036 DeletePendingObjects() ;
1037 wxMacProcessNotifierAndPendingEvents() ;
1040 void wxApp::MacHandleOneEvent( EventRecord
*ev
)
1042 m_macCurrentEvent
= ev
;
1044 wxApp::sm_lastMessageTime
= ev
->when
;
1049 MacHandleMouseDownEvent( ev
) ;
1050 if ( ev
->modifiers
& controlKey
)
1051 s_lastMouseDown
= 2;
1053 s_lastMouseDown
= 1;
1056 if ( s_lastMouseDown
== 2 )
1058 ev
->modifiers
|= controlKey
;
1062 ev
->modifiers
&= ~controlKey
;
1064 MacHandleMouseUpEvent( ev
) ;
1065 s_lastMouseDown
= 0;
1068 MacHandleActivateEvent( ev
) ;
1071 MacHandleUpdateEvent( ev
) ;
1075 MacHandleKeyDownEvent( ev
) ;
1078 MacHandleKeyUpEvent( ev
) ;
1081 MacHandleDiskEvent( ev
) ;
1084 MacHandleOSEvent( ev
) ;
1086 case kHighLevelEvent
:
1087 MacHandleHighLevelEvent( ev
) ;
1092 wxMacProcessNotifierAndPendingEvents() ;
1095 void wxApp::MacHandleHighLevelEvent( EventRecord
*ev
)
1097 ::AEProcessAppleEvent( ev
) ;
1100 bool s_macIsInModalLoop
= false ;
1102 void wxApp::MacHandleMouseDownEvent( EventRecord
*ev
)
1104 wxToolTip::RemoveToolTips() ;
1107 WindowRef frontWindow
= ::FrontNonFloatingWindow() ;
1108 WindowAttributes frontWindowAttributes
= NULL
;
1110 ::GetWindowAttributes( frontWindow
, &frontWindowAttributes
) ;
1112 short windowPart
= ::FindWindow(ev
->where
, &window
);
1113 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
1114 if ( wxPendingDelete
.Member(win
) )
1118 GetQDGlobalsScreenBits( &screenBits
);
1123 if ( s_macIsInModalLoop
)
1129 UInt32 menuresult
= MenuSelect(ev
->where
) ;
1130 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) );
1131 s_lastMouseDown
= 0;
1136 SystemClick( ev
, window
) ;
1137 s_lastMouseDown
= 0;
1141 if ( window
!= frontWindow
&& s_macIsInModalLoop
&& !(ev
->modifiers
& cmdKey
) )
1147 DragWindow(window
, ev
->where
, &screenBits
.bounds
);
1152 Point pt
= { 0, 0 } ;
1154 SetPort( GetWindowPort(window
) ) ;
1156 SetPort( (window
) ) ;
1158 SetOrigin( 0 , 0 ) ;
1159 LocalToGlobal( &pt
) ;
1161 win
->SetSize( pt
.h
, pt
.v
, -1 ,
1162 -1 , wxSIZE_USE_EXISTING
);
1164 s_lastMouseDown
= 0;
1168 if (TrackGoAway(window
, ev
->where
))
1173 s_lastMouseDown
= 0;
1177 int growResult
= GrowWindow(window
, ev
->where
, &screenBits
.bounds
);
1178 if (growResult
!= 0)
1180 int newWidth
= LoWord(growResult
);
1181 int newHeight
= HiWord(growResult
);
1182 int oldWidth
, oldHeight
;
1187 win
->GetSize(&oldWidth
, &oldHeight
);
1189 newWidth
= oldWidth
;
1191 newHeight
= oldHeight
;
1192 win
->SetSize( -1, -1, newWidth
, newHeight
, wxSIZE_USE_EXISTING
);
1195 s_lastMouseDown
= 0;
1200 if (TrackBox(window
, ev
->where
, windowPart
))
1202 // TODO setup size event
1203 ZoomWindow( window
, windowPart
, false ) ;
1208 GetWindowPortBounds(window
, &tempRect
) ;
1209 win
->SetSize( -1, -1, tempRect
.right
-tempRect
.left
,
1210 tempRect
.bottom
-tempRect
.top
, wxSIZE_USE_EXISTING
);
1213 s_lastMouseDown
= 0;
1215 case inCollapseBox
:
1216 // TODO setup size event
1217 s_lastMouseDown
= 0;
1225 SetPort( GetWindowPort(window
) ) ;
1227 SetPort( (window
) ) ;
1229 SetOrigin( 0 , 0 ) ;
1232 if ( window
!= frontWindow
&& wxTheApp
->s_captureWindow
== NULL
)
1234 if ( s_macIsInModalLoop
)
1238 else if ( UMAIsWindowFloating( window
) )
1241 win
->MacMouseDown( ev
, windowPart
) ;
1246 win
->MacMouseDown( ev
, windowPart
) ;
1247 ::SelectWindow( window
) ;
1253 win
->MacMouseDown( ev
, windowPart
) ;
1262 void wxApp::MacHandleMouseUpEvent( EventRecord
*ev
)
1266 short windowPart
= ::FindWindow(ev
->where
, &window
);
1276 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
1278 win
->MacMouseUp( ev
, windowPart
) ;
1284 long wxMacTranslateKey(unsigned char key
, unsigned char code
) ;
1285 long wxMacTranslateKey(unsigned char key
, unsigned char code
)
1294 retval
= WXK_RETURN
;
1309 retval
= WXK_PAGEUP
;
1312 retval
= WXK_PAGEDOWN
;
1315 retval
= WXK_RETURN
;
1370 retval
= WXK_ESCAPE
;
1376 retval
= WXK_RIGHT
;
1385 retval
= WXK_DELETE
;
1393 void wxApp::MacHandleKeyDownEvent( EventRecord
*ev
)
1395 wxToolTip::RemoveToolTips() ;
1397 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1398 if ( HiWord( menuresult
) )
1400 if ( !s_macIsInModalLoop
)
1401 MacHandleMenuSelect( HiWord( menuresult
) , LoWord( menuresult
) ) ;
1407 keychar
= short(ev
->message
& charCodeMask
);
1408 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1410 wxWindow
* focus
= wxWindow::FindFocus() ;
1413 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1415 wxKeyEvent
event(wxEVT_KEY_DOWN
);
1416 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1417 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1418 event
.m_altDown
= ev
->modifiers
& optionKey
;
1419 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1420 event
.m_keyCode
= keyval
;
1421 event
.m_x
= ev
->where
.h
;
1422 event
.m_y
= ev
->where
.v
;
1423 event
.m_timeStamp
= ev
->when
;
1424 event
.SetEventObject(focus
);
1425 bool handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1431 wxWindow
*ancestor
= focus
;
1435 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1438 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1439 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1442 if (ancestor->m_isFrame)
1444 ancestor = ancestor->GetParent();
1448 #endif // wxUSE_ACCEL
1452 wxKeyEvent
event(wxEVT_CHAR
);
1453 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1454 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1455 event
.m_altDown
= ev
->modifiers
& optionKey
;
1456 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1457 event
.m_keyCode
= keyval
;
1458 event
.m_x
= ev
->where
.h
;
1459 event
.m_y
= ev
->where
.v
;
1460 event
.m_timeStamp
= ev
->when
;
1461 event
.SetEventObject(focus
);
1462 handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1465 (keyval
== WXK_TAB
) &&
1466 (!focus
->HasFlag(wxTE_PROCESS_TAB
)) &&
1467 (focus
->GetParent()) &&
1468 (focus
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
1470 wxNavigationKeyEvent new_event
;
1471 new_event
.SetEventObject( focus
);
1472 new_event
.SetDirection( !event
.ShiftDown() );
1473 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1474 new_event
.SetWindowChange( event
.ControlDown() );
1475 new_event
.SetCurrentFocus( focus
);
1476 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1478 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1480 (keyval
== '.' && event
.ControlDown() ) )
1482 wxCommandEvent
new_event(wxEVT_COMMAND_BUTTON_CLICKED
,wxID_CANCEL
);
1483 new_event
.SetEventObject( focus
);
1484 handled
= focus
->GetEventHandler()->ProcessEvent( new_event
);
1490 void wxApp::MacHandleKeyUpEvent( EventRecord
*ev
)
1492 wxToolTip::RemoveToolTips() ;
1494 UInt32 menuresult
= UMAMenuEvent(ev
) ;
1495 if ( HiWord( menuresult
) )
1502 keychar
= short(ev
->message
& charCodeMask
);
1503 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1505 wxWindow
* focus
= wxWindow::FindFocus() ;
1508 long keyval
= wxMacTranslateKey(keychar
, keycode
) ;
1510 wxKeyEvent
event(wxEVT_KEY_UP
);
1511 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1512 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1513 event
.m_altDown
= ev
->modifiers
& optionKey
;
1514 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1515 event
.m_keyCode
= keyval
;
1516 event
.m_x
= ev
->where
.h
;
1517 event
.m_y
= ev
->where
.v
;
1518 event
.m_timeStamp
= ev
->when
;
1519 event
.SetEventObject(focus
);
1520 bool handled
= focus
->GetEventHandler()->ProcessEvent( event
) ;
1525 void wxApp::MacHandleActivateEvent( EventRecord
*ev
)
1527 WindowRef window
= (WindowRef
) ev
->message
;
1530 bool activate
= (ev
->modifiers
& activeFlag
) ;
1531 WindowClass wclass
;
1532 ::GetWindowClass ( window
, &wclass
) ;
1533 if ( wclass
== kFloatingWindowClass
)
1535 // if it is a floater we activate/deactivate the front non-floating window instead
1536 window
= ::FrontNonFloatingWindow() ;
1538 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
1540 win
->MacActivate( ev
, activate
) ;
1544 void wxApp::MacHandleUpdateEvent( EventRecord
*ev
)
1546 WindowRef window
= (WindowRef
) ev
->message
;
1547 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
1550 if ( !wxPendingDelete
.Member(win
) )
1551 win
->MacUpdate( ev
) ;
1555 // since there is no way of telling this foreign window to update itself
1556 // we have to invalidate the update region otherwise we keep getting the same
1557 // event over and over again
1558 BeginUpdate( window
) ;
1559 EndUpdate( window
) ;
1563 void wxApp::MacHandleDiskEvent( EventRecord
*ev
)
1565 if ( HiWord( ev
->message
) != noErr
)
1570 SetPt( &point
, 100 , 100 ) ;
1572 err
= DIBadMount( point
, ev
->message
) ;
1573 wxASSERT( err
== noErr
) ;
1578 void wxApp::MacHandleOSEvent( EventRecord
*ev
)
1580 switch( ( ev
->message
& osEvtMessageMask
) >> 24 )
1582 case suspendResumeMessage
:
1584 bool isResuming
= ev
->message
& resumeFlag
;
1586 bool convertClipboard
= ev
->message
& convertClipboardFlag
;
1588 bool convertClipboard
= false;
1590 bool doesActivate
= UMAGetProcessModeDoesActivateOnFGSwitch() ;
1593 WindowRef oldFrontWindow
= NULL
;
1594 WindowRef newFrontWindow
= NULL
;
1596 // in case we don't take care of activating ourselves, we have to synchronize
1597 // our idea of the active window with the process manager's - which it already activated
1599 if ( !doesActivate
)
1600 oldFrontWindow
= ::FrontNonFloatingWindow() ;
1602 MacResume( convertClipboard
) ;
1604 newFrontWindow
= ::FrontNonFloatingWindow() ;
1606 if ( oldFrontWindow
)
1608 wxWindow
* win
= wxFindWinFromMacWindow( oldFrontWindow
) ;
1610 win
->MacActivate( ev
, false ) ;
1612 if ( newFrontWindow
)
1614 wxWindow
* win
= wxFindWinFromMacWindow( newFrontWindow
) ;
1616 win
->MacActivate( ev
, true ) ;
1621 MacSuspend( convertClipboard
) ;
1623 // in case this suspending did close an active window, another one might
1624 // have surfaced -> lets deactivate that one
1626 /* TODO : find out what to do on systems < 10 , perhaps FrontNonFloatingWindow
1627 WindowRef newActiveWindow = ::ActiveNonFloatingWindow() ;
1628 if ( newActiveWindow )
1630 wxWindow* win = wxFindWinFromMacWindow( newActiveWindow ) ;
1632 win->MacActivate( ev , false ) ;
1638 case mouseMovedMessage
:
1642 wxWindow
* currentMouseWindow
= NULL
;
1644 wxWindow::MacGetWindowFromPoint( wxPoint( ev
->where
.h
, ev
->where
.v
) ,
1645 ¤tMouseWindow
) ;
1647 if ( currentMouseWindow
!= wxWindow::s_lastMouseWindow
)
1649 wxMouseEvent event
;
1651 bool isDown
= !(ev
->modifiers
& btnState
) ; // 1 is for up
1652 bool controlDown
= ev
->modifiers
& controlKey
; // for simulating right mouse
1654 event
.m_leftDown
= isDown
&& !controlDown
;
1655 event
.m_middleDown
= FALSE
;
1656 event
.m_rightDown
= isDown
&& controlDown
;
1657 event
.m_shiftDown
= ev
->modifiers
& shiftKey
;
1658 event
.m_controlDown
= ev
->modifiers
& controlKey
;
1659 event
.m_altDown
= ev
->modifiers
& optionKey
;
1660 event
.m_metaDown
= ev
->modifiers
& cmdKey
;
1661 event
.m_x
= ev
->where
.h
;
1662 event
.m_y
= ev
->where
.v
;
1663 event
.m_timeStamp
= ev
->when
;
1664 event
.SetEventObject(this);
1666 if ( wxWindow::s_lastMouseWindow
)
1668 wxMouseEvent
eventleave(event
) ;
1669 eventleave
.SetEventType( wxEVT_LEAVE_WINDOW
) ;
1670 wxWindow::s_lastMouseWindow
->GetEventHandler()->ProcessEvent(eventleave
);
1672 if ( currentMouseWindow
)
1674 wxMouseEvent
evententer(event
) ;
1675 evententer
.SetEventType( wxEVT_ENTER_WINDOW
) ;
1676 currentMouseWindow
->GetEventHandler()->ProcessEvent(evententer
);
1678 wxWindow::s_lastMouseWindow
= currentMouseWindow
;
1681 short windowPart
= ::FindWindow(ev
->where
, &window
);
1685 // fixes for setting the cursor back from dominic mazzoni
1687 UMAShowArrowCursor();
1690 UMAShowArrowCursor();
1694 // if ( s_lastMouseDown == 0 )
1695 // ev->modifiers |= btnState ;
1697 // Calling GetNextEvent with a zero event mask will always
1698 // pass back a null event. However, it fills the EventRecord
1699 // with the state of the modifier keys. This is needed since
1700 // the modifier state returned by WaitForNextEvent often is
1701 // wrong mouse move events. The attempt above to correct this
1702 // didn't always work (under OS X at least).
1705 ::GetNextEvent(0, &tmp
);
1706 ev
->modifiers
= tmp
.modifiers
;
1708 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
1710 win
->MacMouseMoved( ev
, windowPart
) ;
1712 UMAShowArrowCursor();
1723 void wxApp::MacHandleMenuSelect( int macMenuId
, int macMenuItemNum
)
1726 return; // no menu item selected
1728 if (macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
> 1)
1731 Str255 deskAccessoryName
;
1734 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId
), macMenuItemNum
, deskAccessoryName
);
1735 GetPort(&savedPort
);
1736 OpenDeskAcc(deskAccessoryName
);
1742 wxWindow
* frontwindow
= wxFindWinFromMacWindow( ::FrontWindow() ) ;
1743 if ( frontwindow
&& wxMenuBar::MacGetInstalledMenuBar() )
1744 wxMenuBar::MacGetInstalledMenuBar()->MacMenuSelect( frontwindow
->GetEventHandler() , 0 , macMenuId
, macMenuItemNum
) ;
1750 long wxApp::MacTranslateKey(char key, int mods)
1754 void wxApp::MacAdjustCursor()
1761 wxApp::macAdjustCursor()
1763 if (ev->what != kHighLevelEvent)
1765 wxWindow* theMacWxFrame = wxFrame::MacFindFrameOrDialog(::FrontWindow());
1768 if (!theMacWxFrame->MacAdjustCursor(ev->where))
1769 ::SetCursor(&(qd.arrow));