]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/carbon/app.cpp
use UNIX wxDir if available
[wxWidgets.git] / src / mac / carbon / app.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: app.cpp
3// Purpose: wxApp
4// Author: AUTHOR
5// Modified by:
6// Created: ??/??/98
7// RCS-ID: $Id$
8// Copyright: (c) AUTHOR
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "app.h"
14#endif
15
16#include "wx/window.h"
17#include "wx/frame.h"
18#include "wx/button.h"
19#include "wx/app.h"
20#include "wx/utils.h"
21#include "wx/gdicmn.h"
22#include "wx/pen.h"
23#include "wx/brush.h"
24#include "wx/cursor.h"
25#include "wx/intl.h"
26#include "wx/icon.h"
27#include "wx/palette.h"
28#include "wx/dc.h"
29#include "wx/dialog.h"
30#include "wx/msgdlg.h"
31#include "wx/log.h"
32#include "wx/module.h"
33#include "wx/memory.h"
34#include "wx/tooltip.h"
35#include "wx/textctrl.h"
36#include "wx/menu.h"
37#if wxUSE_WX_RESOURCES
38#include "wx/resource.h"
39#endif
40
41#include <string.h>
42
43// mac
44
45#ifndef __DARWIN__
46 #if __option(profile)
47 #include <profiler.h>
48 #endif
49#endif
50
51#include "apprsrc.h"
52
53#include "wx/mac/uma.h"
54#include "wx/mac/macnotfy.h"
55
56#if wxUSE_SOCKETS
57 #ifdef __DARWIN__
58 #include <CoreServices/CoreServices.h>
59 #endif
60#endif
61
62extern char *wxBuffer;
63extern wxList wxPendingDelete;
64extern wxList *wxWinMacWindowList;
65extern wxList *wxWinMacControlList;
66
67wxApp *wxTheApp = NULL;
68
69#if !USE_SHARED_LIBRARY
70IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
71BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
72 EVT_IDLE(wxApp::OnIdle)
73 EVT_END_SESSION(wxApp::OnEndSession)
74 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
75END_EVENT_TABLE()
76#endif
77
78
79const short kMacMinHeap = (29 * 1024) ;
80// platform specific static variables
81
82const short kwxMacMenuBarResource = 1 ;
83const short kwxMacAppleMenuId = 1 ;
84
85WXHRGN wxApp::s_macCursorRgn = NULL;
86wxWindow* wxApp::s_captureWindow = NULL ;
87int wxApp::s_lastMouseDown = 0 ;
88long wxApp::sm_lastMessageTime = 0;
89
90bool wxApp::s_macDefaultEncodingIsPC = true ;
91bool wxApp::s_macSupportPCMenuShortcuts = true ;
92long wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
93wxString wxApp::s_macHelpMenuTitleName = "&Help" ;
94
95pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
96pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
97pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
98pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
99
100
101pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long refcon )
102{
103 wxApp* app = (wxApp*) refcon ;
104 return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ;
105}
106
107pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon )
108{
109 wxApp* app = (wxApp*) refcon ;
110 return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ;
111}
112
113pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon )
114{
115 wxApp* app = (wxApp*) refcon ;
116 return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ;
117}
118
119pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon )
120{
121 wxApp* app = (wxApp*) refcon ;
122 return wxTheApp->MacHandleAEQuit( (AppleEvent*) event , reply) ;
123}
124
125short wxApp::MacHandleAEODoc(const WXEVENTREF event , WXEVENTREF reply)
126{
127 SysBeep(40) ;
128 ProcessSerialNumber PSN ;
129 PSN.highLongOfPSN = 0 ;
130 PSN.lowLongOfPSN = kCurrentProcess ;
131 SetFrontProcess( &PSN ) ;
132 return noErr ;
133}
134
135short wxApp::MacHandleAEPDoc(const WXEVENTREF event , WXEVENTREF reply)
136{
137 return noErr ;
138}
139
140short wxApp::MacHandleAEOApp(const WXEVENTREF event , WXEVENTREF reply)
141{
142 return noErr ;
143}
144
145short wxApp::MacHandleAEQuit(const WXEVENTREF event , WXEVENTREF reply)
146{
147 wxWindow* win = GetTopWindow() ;
148 if ( win )
149 {
150 win->Close(TRUE ) ;
151 }
152 else
153 {
154 ExitMainLoop() ;
155 }
156 return noErr ;
157}
158
159char StringMac[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
160 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
161 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf"
162 "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf"
163 "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf"
164 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ;
165
166char StringANSI[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8"
167 "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC"
168 "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8"
169 "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8"
170 "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C"
171 "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ;
172
173void wxMacConvertFromPC( const char *from , char *to , int len )
174{
175 char *c ;
176 if ( from == to )
177 {
178 for( int i = 0 ; i < len ; ++ i )
179 {
180 c = strchr( StringANSI , *from ) ;
181 if ( c != NULL )
182 {
183 *to = StringMac[ c - StringANSI] ;
184 }
185 ++to ;
186 ++from ;
187 }
188 }
189 else
190 {
191 for( int i = 0 ; i < len ; ++ i )
192 {
193 c = strchr( StringANSI , *from ) ;
194 if ( c != NULL )
195 {
196 *to = StringMac[ c - StringANSI] ;
197 }
198 else
199 {
200 *to = *from ;
201 }
202 ++to ;
203 ++from ;
204 }
205 }
206}
207
208void wxMacConvertToPC( const char *from , char *to , int len )
209{
210 char *c ;
211 if ( from == to )
212 {
213 for( int i = 0 ; i < len ; ++ i )
214 {
215 c = strchr( StringMac , *from ) ;
216 if ( c != NULL )
217 {
218 *to = StringANSI[ c - StringMac] ;
219 }
220 ++to ;
221 ++from ;
222 }
223 }
224 else
225 {
226 for( int i = 0 ; i < len ; ++ i )
227 {
228 c = strchr( StringMac , *from ) ;
229 if ( c != NULL )
230 {
231 *to = StringANSI[ c - StringMac] ;
232 }
233 else
234 {
235 *to = *from ;
236 }
237 ++to ;
238 ++from ;
239 }
240 }
241}
242
243void wxMacConvertFromPC( char * p )
244{
245 char *ptr = p ;
246 int len = strlen ( p ) ;
247
248 wxMacConvertFromPC( ptr , ptr , len ) ;
249}
250
251void wxMacConvertFromPCForControls( char * p )
252{
253 char *ptr = p ;
254 int len = strlen ( p ) ;
255
256 wxMacConvertFromPC( ptr , ptr , len ) ;
257 for ( int i = 0 ; i < strlen ( ptr ) ; i++ )
258 {
259 if ( ptr[i] == '&' && ptr[i]+1 != ' ' )
260 {
261 memmove( &ptr[i] , &ptr[i+1] , strlen( &ptr[i+1] ) + 1) ;
262 }
263 }
264}
265
266void wxMacConvertFromPC( unsigned char *p )
267{
268 char *ptr = (char*) p + 1 ;
269 int len = p[0] ;
270
271 wxMacConvertFromPC( ptr , ptr , len ) ;
272}
273
274extern char *wxBuffer ;
275
276wxString wxMacMakeMacStringFromPC( const char * p )
277{
278 const char *ptr = p ;
279 int len = strlen ( p ) ;
280 char *buf = wxBuffer ;
281
282 if ( len >= BUFSIZ + 512 )
283 {
284 buf = new char [len+1] ;
285 }
286
287 wxMacConvertFromPC( ptr , buf , len ) ;
288 buf[len] = 0 ;
289 wxString result( buf ) ;
290 if ( buf != wxBuffer )
291 delete buf ;
292 return result ;
293}
294
295
296void wxMacConvertToPC( char * p )
297{
298 char *ptr = p ;
299 int len = strlen ( p ) ;
300
301 wxMacConvertToPC( ptr , ptr , len ) ;
302}
303
304void wxMacConvertToPC( unsigned char *p )
305{
306 char *ptr = (char*) p + 1 ;
307 int len = p[0] ;
308
309 wxMacConvertToPC( ptr , ptr , len ) ;
310}
311
312wxString wxMacMakePCStringFromMac( const char * p )
313{
314 const char *ptr = p ;
315 int len = strlen ( p ) ;
316 char *buf = wxBuffer ;
317
318 if ( len >= BUFSIZ + 512 )
319 {
320 buf = new char [len+1] ;
321 }
322
323 wxMacConvertToPC( ptr , buf , len ) ;
324 buf[len] = 0 ;
325
326 wxString result( buf ) ;
327 if ( buf != wxBuffer )
328 delete buf ;
329 return result ;
330}
331
332wxString wxMacMakeStringFromMacString( const char* from , bool mac2pcEncoding )
333{
334 if (mac2pcEncoding)
335 {
336 return wxMacMakePCStringFromMac( from ) ;
337 }
338 else
339 {
340 return wxString( from ) ;
341 }
342}
343
344wxString wxMacMakeStringFromPascal( StringPtr from , bool mac2pcEncoding )
345{
346 // this is safe since a pascal string can never be larger than 256 bytes
347 char s[256] ;
348 CopyPascalStringToC( from , s ) ;
349 if (mac2pcEncoding)
350 {
351 return wxMacMakePCStringFromMac( s ) ;
352 }
353 else
354 {
355 return wxString( s ) ;
356 }
357}
358
359void wxMacStringToPascal( const char * from , StringPtr to , bool pc2macEncoding )
360{
361 if (pc2macEncoding)
362 {
363 CopyCStringToPascal( wxMacMakeMacStringFromPC( from ) , to ) ;
364 }
365 else
366 {
367 CopyCStringToPascal( from , to ) ;
368 }
369}
370
371bool wxApp::Initialize()
372{
373 int error = 0 ;
374
375 // Mac-specific
376
377 UMAInitToolbox( 4 ) ;
378 SetEventMask( everyEvent ) ;
379 UMAShowWatchCursor() ;
380
381#if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
382 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments , NewAEEventHandlerUPP(AEHandleODoc) ,
383 (long) wxTheApp , FALSE ) ;
384 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication , NewAEEventHandlerUPP(AEHandleOApp) ,
385 (long) wxTheApp , FALSE ) ;
386 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments , NewAEEventHandlerUPP(AEHandlePDoc) ,
387 (long) wxTheApp , FALSE ) ;
388 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication , NewAEEventHandlerUPP(AEHandleQuit) ,
389 (long) wxTheApp , FALSE ) ;
390#else
391 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments , NewAEEventHandlerProc(AEHandleODoc) ,
392 (long) wxTheApp , FALSE ) ;
393 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication , NewAEEventHandlerProc(AEHandleOApp) ,
394 (long) wxTheApp , FALSE ) ;
395 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments , NewAEEventHandlerProc(AEHandlePDoc) ,
396 (long) wxTheApp , FALSE ) ;
397 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication , NewAEEventHandlerProc(AEHandleQuit) ,
398 (long) wxTheApp , FALSE ) ;
399#endif
400
401
402#ifndef __DARWIN__
403 // test the minimal configuration necessary
404
405 #if !TARGET_CARBON
406 long theSystem ;
407 long theMachine;
408
409 if (Gestalt(gestaltMachineType, &theMachine) != noErr)
410 {
411 error = kMacSTRWrongMachine;
412 }
413 else if (theMachine < gestaltMacPlus)
414 {
415 error = kMacSTRWrongMachine;
416 }
417 else if (Gestalt(gestaltSystemVersion, &theSystem) != noErr )
418 {
419 error = kMacSTROldSystem ;
420 }
421 else if ( theSystem < 0x0860 )
422 {
423 error = kMacSTROldSystem ;
424 }
425 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap)
426 {
427 error = kMacSTRSmallSize;
428 }
429 #endif
430 /*
431 else
432 {
433 if ( !UMAHasAppearance() )
434 {
435 error = kMacSTRNoPre8Yet ;
436 }
437 }
438 */
439#endif
440
441 // if we encountered any problems so far, give the error code and exit immediately
442
443 if ( error )
444 {
445 short itemHit;
446 Str255 message;
447
448 GetIndString(message, 128, error);
449 UMAShowArrowCursor() ;
450 ParamText("\pFatal Error", message, (ConstStr255Param)"\p", (ConstStr255Param)"\p");
451 itemHit = Alert(128, nil);
452 return FALSE ;
453 }
454
455#ifndef __DARWIN__
456 #if __option(profile)
457 ProfilerInit( collectDetailed, bestTimeBase , 20000 , 40 ) ;
458 #endif
459#endif
460
461 // now avoid exceptions thrown for new (bad_alloc)
462
463#ifndef __DARWIN__
464 std::__throws_bad_alloc = FALSE ;
465#endif
466
467 s_macCursorRgn = ::NewRgn() ;
468
469#ifdef __WXMSW__
470 wxBuffer = new char[1500];
471#else
472 wxBuffer = new char[BUFSIZ + 512];
473#endif
474
475 wxClassInfo::InitializeClasses();
476
477#if wxUSE_RESOURCES
478// wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
479#endif
480
481#if wxUSE_THREADS
482 wxPendingEventsLocker = new wxCriticalSection;
483#endif
484 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
485 wxTheColourDatabase->Initialize();
486
487#ifdef __WXDEBUG__
488#if wxUSE_LOG
489 // flush the logged messages if any and install a 'safer' log target: the
490 // default one (wxLogGui) can't be used after the resources are freed just
491 // below and the user suppliedo ne might be even more unsafe (using any
492 // wxWindows GUI function is unsafe starting from now)
493 wxLog::DontCreateOnDemand();
494
495 // this will flush the old messages if any
496 delete wxLog::SetActiveTarget(new wxLogStderr);
497#endif // wxUSE_LOG
498#endif
499
500 wxInitializeStockLists();
501 wxInitializeStockObjects();
502
503#if wxUSE_WX_RESOURCES
504 wxInitializeResourceSystem();
505#endif
506
507 wxBitmap::InitStandardHandlers();
508
509 wxModule::RegisterModules();
510 if (!wxModule::InitializeModules()) {
511 return FALSE;
512 }
513
514 wxWinMacWindowList = new wxList(wxKEY_INTEGER);
515 wxWinMacControlList = new wxList(wxKEY_INTEGER);
516
517 wxMacCreateNotifierTable() ;
518
519 UMAShowArrowCursor() ;
520
521 return TRUE;
522}
523
524void wxApp::CleanUp()
525{
526 wxToolTip::RemoveToolTips() ;
527#if wxUSE_LOG
528 // flush the logged messages if any and install a 'safer' log target: the
529 // default one (wxLogGui) can't be used after the resources are freed just
530 // below and the user suppliedo ne might be even more unsafe (using any
531 // wxWindows GUI function is unsafe starting from now)
532 wxLog::DontCreateOnDemand();
533
534 // this will flush the old messages if any
535 delete wxLog::SetActiveTarget(new wxLogStderr);
536#endif // wxUSE_LOG
537
538 // One last chance for pending objects to be cleaned up
539 wxTheApp->DeletePendingObjects();
540
541 wxModule::CleanUpModules();
542
543#if wxUSE_WX_RESOURCES
544 wxCleanUpResourceSystem();
545#endif
546
547 wxDeleteStockObjects() ;
548
549 // Destroy all GDI lists, etc.
550 wxDeleteStockLists();
551
552 delete wxTheColourDatabase;
553 wxTheColourDatabase = NULL;
554
555 wxBitmap::CleanUpHandlers();
556
557 delete[] wxBuffer;
558 wxBuffer = NULL;
559
560 wxMacDestroyNotifierTable() ;
561 if (wxWinMacWindowList)
562 delete wxWinMacWindowList ;
563
564 if (wxWinMacControlList)
565 delete wxWinMacControlList ;
566
567 delete wxPendingEvents;
568#if wxUSE_THREADS
569 delete wxPendingEventsLocker;
570 // If we don't do the following, we get an apparent memory leak.
571 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
572#endif
573
574 wxClassInfo::CleanUpClasses();
575
576#ifndef __DARWIN__
577 #if __option(profile)
578 ProfilerDump( "\papp.prof" ) ;
579 ProfilerTerm() ;
580 #endif
581#endif
582
583 delete wxTheApp;
584 wxTheApp = NULL;
585
586#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
587 // At this point we want to check if there are any memory
588 // blocks that aren't part of the wxDebugContext itself,
589 // as a special case. Then when dumping we need to ignore
590 // wxDebugContext, too.
591 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
592 {
593 wxLogDebug(wxT("There were memory leaks."));
594 wxDebugContext::Dump();
595 wxDebugContext::PrintStatistics();
596 }
597 // wxDebugContext::SetStream(NULL, NULL);
598#endif
599
600#if wxUSE_LOG
601 // do it as the very last thing because everything else can log messages
602 delete wxLog::SetActiveTarget(NULL);
603#endif // wxUSE_LOG
604
605 UMACleanupToolbox() ;
606 if (s_macCursorRgn)
607 ::DisposeRgn((RgnHandle)s_macCursorRgn);
608
609 #if 0
610 TerminateAE() ;
611 #endif
612}
613
614//----------------------------------------------------------------------
615// wxEntry
616//----------------------------------------------------------------------
617
618int wxEntryStart( int argc, char *argv[] )
619{
620 return wxApp::Initialize();
621}
622
623
624int wxEntryInitGui()
625{
626 return wxTheApp->OnInitGui();
627}
628
629
630void wxEntryCleanup()
631{
632 wxApp::CleanUp();
633}
634
635
636int wxEntry( int argc, char *argv[] , bool enterLoop )
637{
638#ifdef __MWERKS__
639#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
640 // This seems to be necessary since there are 'rogue'
641 // objects present at this point (perhaps global objects?)
642 // Setting a checkpoint will ignore them as far as the
643 // memory checking facility is concerned.
644 // Of course you may argue that memory allocated in globals should be
645 // checked, but this is a reasonable compromise.
646 wxDebugContext::SetCheckpoint();
647#endif
648#endif
649 if (!wxEntryStart(argc, argv)) {
650 return 0;
651 }
652 // create the application object or ensure that one already exists
653 if (!wxTheApp)
654 {
655 // The app may have declared a global application object, but we recommend
656 // the IMPLEMENT_APP macro is used instead, which sets an initializer
657 // function for delayed, dynamic app object construction.
658 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
659 wxT("No initializer - use IMPLEMENT_APP macro.") );
660
661 wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) ();
662 }
663
664 wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
665
666#ifndef __DARWIN__
667 argc = 0 ; // currently we don't support files as parameters
668#endif
669 // we could try to get the open apple events here to adjust argc and argv better
670
671 wxTheApp->argc = argc;
672 wxTheApp->argv = argv;
673
674 // GUI-specific initialization, such as creating an app context.
675 wxEntryInitGui();
676
677
678 // Here frames insert themselves automatically
679 // into wxTopLevelWindows by getting created
680 // in OnInit().
681
682 int retValue = 0;
683
684 if ( wxTheApp->OnInit() )
685 {
686 if ( enterLoop )
687 {
688 retValue = wxTheApp->OnRun();
689 }
690 else
691 // We want to initialize, but not run or exit immediately.
692 return 1;
693 }
694 //else: app initialization failed, so we skipped OnRun()
695
696 wxWindow *topWindow = wxTheApp->GetTopWindow();
697 if ( topWindow )
698 {
699 // Forcibly delete the window.
700 if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
701 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
702 {
703 topWindow->Close(TRUE);
704 wxTheApp->DeletePendingObjects();
705 }
706 else
707 {
708 delete topWindow;
709 wxTheApp->SetTopWindow(NULL);
710 }
711 }
712
713 wxTheApp->OnExit();
714
715 wxEntryCleanup();
716
717 return retValue;
718}
719
720// Static member initialization
721wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
722
723wxApp::wxApp()
724{
725 m_topWindow = NULL;
726 wxTheApp = this;
727
728 m_wantDebugOutput = TRUE ;
729
730 argc = 0;
731 argv = NULL;
732
733 m_printMode = wxPRINT_WINDOWS;
734 m_exitOnFrameDelete = TRUE;
735 m_auto3D = TRUE;
736}
737
738bool wxApp::Initialized()
739{
740 if (GetTopWindow())
741 return TRUE;
742 else
743 return FALSE;
744}
745
746int wxApp::MainLoop()
747{
748 m_keepGoing = TRUE;
749
750 while (m_keepGoing)
751 {
752 MacDoOneEvent() ;
753 }
754
755 return 0;
756}
757
758// Returns TRUE if more time is needed.
759bool wxApp::ProcessIdle()
760{
761 wxIdleEvent event;
762 event.SetEventObject(this);
763 ProcessEvent(event);
764
765 return event.MoreRequested();
766}
767
768void wxApp::ExitMainLoop()
769{
770 m_keepGoing = FALSE;
771}
772
773// Is a message/event pending?
774bool wxApp::Pending()
775{
776 EventRecord event ;
777
778 return EventAvail( everyEvent , &event ) ;
779}
780
781// Dispatch a message.
782void wxApp::Dispatch()
783{
784 MacDoOneEvent() ;
785}
786
787void wxApp::OnIdle(wxIdleEvent& event)
788{
789 static bool s_inOnIdle = FALSE;
790
791 // Avoid recursion (via ProcessEvent default case)
792 if ( s_inOnIdle )
793 return;
794
795
796 s_inOnIdle = TRUE;
797
798 // 'Garbage' collection of windows deleted with Close().
799 DeletePendingObjects();
800
801 // flush the logged messages if any
802 wxLog *pLog = wxLog::GetActiveTarget();
803 if ( pLog != NULL && pLog->HasPendingMessages() )
804 pLog->Flush();
805
806 // Send OnIdle events to all windows
807 bool needMore = SendIdleEvents();
808
809 if (needMore)
810 event.RequestMore(TRUE);
811
812 // If they are pending events, we must process them: pending events are
813 // either events to the threads other than main or events posted with
814 // wxPostEvent() functions
815 wxMacProcessNotifierAndPendingEvents();
816
817 s_inOnIdle = FALSE;
818}
819
820void wxWakeUpIdle()
821{
822 wxMacWakeUp() ;
823}
824
825// Send idle event to all top-level windows
826bool wxApp::SendIdleEvents()
827{
828 bool needMore = FALSE;
829 wxNode* node = wxTopLevelWindows.First();
830 while (node)
831 {
832 wxWindow* win = (wxWindow*) node->Data();
833 if (SendIdleEvents(win))
834 needMore = TRUE;
835
836 node = node->Next();
837 }
838 return needMore;
839}
840
841// Send idle event to window and all subwindows
842bool wxApp::SendIdleEvents(wxWindow* win)
843{
844 bool needMore = FALSE;
845
846 wxIdleEvent event;
847 event.SetEventObject(win);
848 win->ProcessEvent(event);
849
850 if (event.MoreRequested())
851 needMore = TRUE;
852
853 wxNode* node = win->GetChildren().First();
854 while (node)
855 {
856 wxWindow* win = (wxWindow*) node->Data();
857 if (SendIdleEvents(win))
858 needMore = TRUE;
859
860 node = node->Next();
861 }
862 return needMore ;
863}
864
865void wxApp::DeletePendingObjects()
866{
867 wxNode *node = wxPendingDelete.First();
868 while (node)
869 {
870 wxObject *obj = (wxObject *)node->Data();
871
872 delete obj;
873
874 if (wxPendingDelete.Member(obj))
875 delete node;
876
877 // Deleting one object may have deleted other pending
878 // objects, so start from beginning of list again.
879 node = wxPendingDelete.First();
880 }
881}
882
883wxIcon
884wxApp::GetStdIcon(int which) const
885{
886 switch(which)
887 {
888 case wxICON_INFORMATION:
889 return wxIcon("wxICON_INFO");
890
891 case wxICON_QUESTION:
892 return wxIcon("wxICON_QUESTION");
893
894 case wxICON_EXCLAMATION:
895 return wxIcon("wxICON_WARNING");
896
897 default:
898 wxFAIL_MSG(wxT("requested non existent standard icon"));
899 // still fall through
900
901 case wxICON_HAND:
902 return wxIcon("wxICON_ERROR");
903 }
904}
905
906void wxExit()
907{
908 wxLogError(_("Fatal error: exiting"));
909
910 wxApp::CleanUp();
911 ::ExitToShell() ;
912}
913
914void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
915{
916 if (GetTopWindow())
917 GetTopWindow()->Close(TRUE);
918}
919
920// Default behaviour: close the application with prompts. The
921// user can veto the close, and therefore the end session.
922void wxApp::OnQueryEndSession(wxCloseEvent& event)
923{
924 if (GetTopWindow())
925 {
926 if (!GetTopWindow()->Close(!event.CanVeto()))
927 event.Veto(TRUE);
928 }
929}
930
931extern "C" void wxCYield() ;
932void wxCYield()
933{
934 wxYield() ;
935}
936
937// Yield to other processes
938
939bool wxApp::Yield(bool onlyIfNeeded)
940{
941 static bool s_inYield = FALSE;
942
943 if (s_inYield)
944 {
945 if ( !onlyIfNeeded )
946 {
947 wxFAIL_MSG( wxT("wxYield called recursively" ) );
948 }
949
950 return FALSE;
951 }
952
953 s_inYield = TRUE;
954
955#if wxUSE_THREADS
956 YieldToAnyThread() ;
957#endif
958 EventRecord event ;
959
960 long sleepTime = 1 ; //::GetCaretTime();
961
962 while ( !wxTheApp->IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, (RgnHandle) wxApp::s_macCursorRgn))
963 {
964 wxTheApp->MacHandleOneEvent( &event );
965 if ( event.what != kHighLevelEvent )
966 SetRectRgn( (RgnHandle) wxApp::s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
967 }
968
969 wxMacProcessNotifierAndPendingEvents() ;
970
971 s_inYield = FALSE;
972
973 return TRUE;
974}
975
976// platform specifics
977
978void wxApp::MacSuspend( bool convertClipboard )
979{
980 // we have to deactive the top level windows manually
981
982 wxNode* node = wxTopLevelWindows.First();
983 while (node)
984 {
985 wxTopLevelWindow* win = (wxTopLevelWindow*) node->Data();
986 win->MacActivate( MacGetCurrentEvent() , false ) ;
987
988 node = node->Next();
989 }
990
991 s_lastMouseDown = 0 ;
992 if( convertClipboard )
993 {
994 MacConvertPrivateToPublicScrap() ;
995 }
996
997 ::HideFloatingWindows() ;
998}
999
1000void wxApp::MacResume( bool convertClipboard )
1001{
1002 s_lastMouseDown = 0 ;
1003 if( convertClipboard )
1004 {
1005 MacConvertPublicToPrivateScrap() ;
1006 }
1007
1008 ::ShowFloatingWindows() ;
1009}
1010
1011void wxApp::MacConvertPrivateToPublicScrap()
1012{
1013}
1014
1015void wxApp::MacConvertPublicToPrivateScrap()
1016{
1017}
1018
1019void wxApp::MacDoOneEvent()
1020{
1021 EventRecord event ;
1022
1023 long sleepTime = 1; // GetCaretTime() / 4 ;
1024
1025 if (WaitNextEvent(everyEvent, &event, sleepTime, (RgnHandle) s_macCursorRgn))
1026 {
1027 MacHandleOneEvent( &event );
1028 }
1029 else
1030 {
1031 // idlers
1032 WindowPtr window = ::FrontWindow() ;
1033 if ( window )
1034 ::IdleControls( window ) ;
1035
1036 wxTheApp->ProcessIdle() ;
1037 }
1038 if ( event.what != kHighLevelEvent )
1039 SetRectRgn( (RgnHandle) s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
1040
1041 // repeaters
1042
1043 DeletePendingObjects() ;
1044 wxMacProcessNotifierAndPendingEvents() ;
1045}
1046
1047void wxApp::MacHandleOneEvent( WXEVENTREF evr )
1048{
1049 EventRecord* ev = (EventRecord*) evr ;
1050 m_macCurrentEvent = ev ;
1051
1052 wxApp::sm_lastMessageTime = ev->when ;
1053
1054 switch (ev->what)
1055 {
1056 case mouseDown:
1057 MacHandleMouseDownEvent( ev ) ;
1058 if ( ev->modifiers & controlKey )
1059 s_lastMouseDown = 2;
1060 else
1061 s_lastMouseDown = 1;
1062 break;
1063 case mouseUp:
1064 if ( s_lastMouseDown == 2 )
1065 {
1066 ev->modifiers |= controlKey ;
1067 }
1068 else
1069 {
1070 ev->modifiers &= ~controlKey ;
1071 }
1072 MacHandleMouseUpEvent( ev ) ;
1073 s_lastMouseDown = 0;
1074 break;
1075 case activateEvt:
1076 MacHandleActivateEvent( ev ) ;
1077 break;
1078 case updateEvt:
1079 MacHandleUpdateEvent( ev ) ;
1080 break;
1081 case keyDown:
1082 case autoKey:
1083 MacHandleKeyDownEvent( ev ) ;
1084 break;
1085 case keyUp:
1086 MacHandleKeyUpEvent( ev ) ;
1087 break;
1088 case diskEvt:
1089 MacHandleDiskEvent( ev ) ;
1090 break;
1091 case osEvt:
1092 MacHandleOSEvent( ev ) ;
1093 break;
1094 case kHighLevelEvent:
1095 MacHandleHighLevelEvent( ev ) ;
1096 break;
1097 default:
1098 break;
1099 }
1100 wxMacProcessNotifierAndPendingEvents() ;
1101}
1102
1103void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr )
1104{
1105 EventRecord* ev = (EventRecord*) evr ;
1106 ::AEProcessAppleEvent( ev ) ;
1107}
1108
1109bool s_macIsInModalLoop = false ;
1110
1111void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr )
1112{
1113 EventRecord* ev = (EventRecord*) evr ;
1114 wxToolTip::RemoveToolTips() ;
1115
1116 WindowRef window;
1117 WindowRef frontWindow = ::FrontNonFloatingWindow() ;
1118 WindowAttributes frontWindowAttributes = NULL ;
1119 if ( frontWindow )
1120 ::GetWindowAttributes( frontWindow , &frontWindowAttributes ) ;
1121
1122 short windowPart = ::FindWindow(ev->where, &window);
1123 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1124 if ( wxPendingDelete.Member(win) )
1125 return ;
1126
1127 BitMap screenBits;
1128 GetQDGlobalsScreenBits( &screenBits );
1129
1130 switch (windowPart)
1131 {
1132 case inMenuBar :
1133 if ( s_macIsInModalLoop )
1134 {
1135 SysBeep ( 30 ) ;
1136 }
1137 else
1138 {
1139 UInt32 menuresult = MenuSelect(ev->where) ;
1140 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) );
1141 s_lastMouseDown = 0;
1142 }
1143 break ;
1144#if !TARGET_CARBON
1145 case inSysWindow :
1146 SystemClick( ev , window ) ;
1147 s_lastMouseDown = 0;
1148 break ;
1149#endif
1150 case inDrag :
1151 if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) )
1152 {
1153 SysBeep ( 30 ) ;
1154 }
1155 else
1156 {
1157 DragWindow(window, ev->where, &screenBits.bounds);
1158 if (win)
1159 {
1160 GrafPtr port ;
1161 GetPort( &port ) ;
1162 Point pt = { 0, 0 } ;
1163 SetPort( GetWindowPort(window) ) ;
1164 LocalToGlobal( &pt ) ;
1165 SetPort( port ) ;
1166 win->SetSize( pt.h , pt.v , -1 ,
1167 -1 , wxSIZE_USE_EXISTING);
1168 }
1169 s_lastMouseDown = 0;
1170 }
1171 break ;
1172 case inGoAway:
1173 if (TrackGoAway(window, ev->where))
1174 {
1175 if ( win )
1176 win->Close() ;
1177 }
1178 s_lastMouseDown = 0;
1179 break;
1180 case inGrow:
1181 {
1182 int growResult = GrowWindow(window , ev->where, &screenBits.bounds);
1183 if (growResult != 0)
1184 {
1185 int newWidth = LoWord(growResult);
1186 int newHeight = HiWord(growResult);
1187 int oldWidth, oldHeight;
1188
1189
1190 if (win)
1191 {
1192 win->GetSize(&oldWidth, &oldHeight);
1193 if (newWidth == 0)
1194 newWidth = oldWidth;
1195 if (newHeight == 0)
1196 newHeight = oldHeight;
1197 win->SetSize( -1, -1, newWidth, newHeight, wxSIZE_USE_EXISTING);
1198 }
1199 }
1200 s_lastMouseDown = 0;
1201 }
1202 break;
1203 case inZoomIn:
1204 case inZoomOut:
1205 if (TrackBox(window, ev->where, windowPart))
1206 {
1207 // TODO setup size event
1208 ZoomWindow( window , windowPart , false ) ;
1209 if (win)
1210 {
1211 Rect tempRect ;
1212
1213 GetWindowPortBounds(window, &tempRect ) ;
1214 win->SetSize( -1, -1, tempRect.right-tempRect.left ,
1215 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
1216 }
1217 }
1218 s_lastMouseDown = 0;
1219 break;
1220 case inCollapseBox :
1221 // TODO setup size event
1222 s_lastMouseDown = 0;
1223 break ;
1224
1225 case inContent :
1226 {
1227 GrafPtr port ;
1228 GetPort( &port ) ;
1229 SetPort( GetWindowPort(window) ) ;
1230 SetPort( port ) ;
1231 }
1232 if ( window != frontWindow && wxTheApp->s_captureWindow == NULL )
1233 {
1234 if ( s_macIsInModalLoop )
1235 {
1236 SysBeep ( 30 ) ;
1237 }
1238 else if ( UMAIsWindowFloating( window ) )
1239 {
1240 if ( win )
1241 win->MacMouseDown( ev , windowPart ) ;
1242 }
1243 else
1244 {
1245 if ( win )
1246 win->MacMouseDown( ev , windowPart ) ;
1247 ::SelectWindow( window ) ;
1248 }
1249 }
1250 else
1251 {
1252 if ( win )
1253 win->MacMouseDown( ev , windowPart ) ;
1254 }
1255 break ;
1256
1257 default:
1258 break;
1259 }
1260}
1261
1262void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr )
1263{
1264 EventRecord* ev = (EventRecord*) evr ;
1265 WindowRef window;
1266
1267 short windowPart = ::FindWindow(ev->where, &window);
1268
1269 switch (windowPart)
1270 {
1271 case inMenuBar :
1272 break ;
1273 case inSysWindow :
1274 break ;
1275 default:
1276 {
1277 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1278 if ( win )
1279 win->MacMouseUp( ev , windowPart ) ;
1280 }
1281 break;
1282 }
1283}
1284
1285long wxMacTranslateKey(unsigned char key, unsigned char code) ;
1286long wxMacTranslateKey(unsigned char key, unsigned char code)
1287{
1288 long retval = key ;
1289 switch (key)
1290 {
1291 case 0x01 :
1292 retval = WXK_HOME;
1293 break;
1294 case 0x03 :
1295 retval = WXK_RETURN;
1296 break;
1297 case 0x04 :
1298 retval = WXK_END;
1299 break;
1300 case 0x05 :
1301 retval = WXK_HELP;
1302 break;
1303 case 0x08 :
1304 retval = WXK_BACK;
1305 break;
1306 case 0x09 :
1307 retval = WXK_TAB;
1308 break;
1309 case 0x0b :
1310 retval = WXK_PAGEUP;
1311 break;
1312 case 0x0c :
1313 retval = WXK_PAGEDOWN;
1314 break;
1315 case 0x0d :
1316 retval = WXK_RETURN;
1317 break;
1318 case 0x10 :
1319 {
1320 switch( code )
1321 {
1322 case 0x7a :
1323 retval = WXK_F1 ;
1324 break;
1325 case 0x78 :
1326 retval = WXK_F2 ;
1327 break;
1328 case 0x63 :
1329 retval = WXK_F3 ;
1330 break;
1331 case 0x76 :
1332 retval = WXK_F4 ;
1333 break;
1334 case 0x60 :
1335 retval = WXK_F5 ;
1336 break;
1337 case 0x61 :
1338 retval = WXK_F6 ;
1339 break;
1340 case 0x62:
1341 retval = WXK_F7 ;
1342 break;
1343 case 0x64 :
1344 retval = WXK_F8 ;
1345 break;
1346 case 0x65 :
1347 retval = WXK_F9 ;
1348 break;
1349 case 0x6D :
1350 retval = WXK_F10 ;
1351 break;
1352 case 0x67 :
1353 retval = WXK_F11 ;
1354 break;
1355 case 0x6F :
1356 retval = WXK_F12 ;
1357 break;
1358 case 0x69 :
1359 retval = WXK_F13 ;
1360 break;
1361 case 0x6B :
1362 retval = WXK_F14 ;
1363 break;
1364 case 0x71 :
1365 retval = WXK_F15 ;
1366 break;
1367 }
1368 }
1369 break ;
1370 case 0x1b :
1371 retval = WXK_ESCAPE ;
1372 break ;
1373 case 0x1c :
1374 retval = WXK_LEFT ;
1375 break ;
1376 case 0x1d :
1377 retval = WXK_RIGHT ;
1378 break ;
1379 case 0x1e :
1380 retval = WXK_UP ;
1381 break ;
1382 case 0x1f :
1383 retval = WXK_DOWN ;
1384 break ;
1385 case 0x7F :
1386 retval = WXK_DELETE ;
1387 default:
1388 break ;
1389 } // end switch
1390
1391 return retval;
1392}
1393
1394void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr )
1395{
1396 EventRecord* ev = (EventRecord*) evr ;
1397 wxToolTip::RemoveToolTips() ;
1398
1399 UInt32 menuresult = UMAMenuEvent(ev) ;
1400 if ( HiWord( menuresult ) )
1401 {
1402 if ( !s_macIsInModalLoop )
1403 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
1404 }
1405 else
1406 {
1407 short keycode ;
1408 short keychar ;
1409 keychar = short(ev->message & charCodeMask);
1410 keycode = short(ev->message & keyCodeMask) >> 8 ;
1411 long keyval = wxMacTranslateKey(keychar, keycode) ;
1412 bool handled = false ;
1413 wxWindow* focus = wxWindow::FindFocus() ;
1414 if ( focus )
1415 {
1416
1417 wxKeyEvent event(wxEVT_KEY_DOWN);
1418 event.m_shiftDown = ev->modifiers & shiftKey;
1419 event.m_controlDown = ev->modifiers & controlKey;
1420 event.m_altDown = ev->modifiers & optionKey;
1421 event.m_metaDown = ev->modifiers & cmdKey;
1422 event.m_keyCode = keyval;
1423 event.m_x = ev->where.h;
1424 event.m_y = ev->where.v;
1425 event.m_timeStamp = ev->when;
1426 event.SetEventObject(focus);
1427 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1428 if ( !handled )
1429 {
1430 #if wxUSE_ACCEL
1431 if (!handled)
1432 {
1433 wxWindow *ancestor = focus;
1434 /*
1435 while (ancestor)
1436 {
1437 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1438 if (command != -1)
1439 {
1440 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1441 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1442 break;
1443 }
1444 if (ancestor->m_isFrame)
1445 break;
1446 ancestor = ancestor->GetParent();
1447 }
1448 */
1449 }
1450 #endif // wxUSE_ACCEL
1451 }
1452 if (!handled)
1453 {
1454 wxKeyEvent event(wxEVT_CHAR);
1455 event.m_shiftDown = ev->modifiers & shiftKey;
1456 event.m_controlDown = ev->modifiers & controlKey;
1457 event.m_altDown = ev->modifiers & optionKey;
1458 event.m_metaDown = ev->modifiers & cmdKey;
1459 event.m_keyCode = keyval;
1460 event.m_x = ev->where.h;
1461 event.m_y = ev->where.v;
1462 event.m_timeStamp = ev->when;
1463 event.SetEventObject(focus);
1464 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1465 }
1466 if ( !handled &&
1467 (keyval == WXK_TAB) &&
1468 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
1469 (focus->GetParent()) &&
1470 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
1471 {
1472 wxNavigationKeyEvent new_event;
1473 new_event.SetEventObject( focus );
1474 new_event.SetDirection( !event.ShiftDown() );
1475 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1476 new_event.SetWindowChange( event.ControlDown() );
1477 new_event.SetCurrentFocus( focus );
1478 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1479 }
1480 }
1481 if ( !handled )
1482 {
1483 // if window is not having a focus still testing for default enter or cancel
1484 // TODO add the UMA version for ActiveNonFloatingWindow
1485 focus = wxFindWinFromMacWindow( FrontWindow() ) ;
1486 if ( focus )
1487 {
1488 if ( keyval == WXK_RETURN )
1489 {
1490 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
1491 wxButton);
1492 if ( def && def->IsEnabled() )
1493 {
1494 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1495 event.SetEventObject(def);
1496 def->Command(event);
1497 return ;
1498 }
1499 }
1500 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1501 else if (keyval == WXK_ESCAPE || (keyval == '.' && ev->modifiers & cmdKey ) )
1502 {
1503 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1504 new_event.SetEventObject( focus );
1505 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1506 }
1507 }
1508 }
1509 }
1510}
1511
1512void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr )
1513{
1514 EventRecord* ev = (EventRecord*) evr ;
1515 wxToolTip::RemoveToolTips() ;
1516
1517 UInt32 menuresult = UMAMenuEvent(ev) ;
1518 if ( HiWord( menuresult ) )
1519 {
1520 }
1521 else
1522 {
1523 short keycode ;
1524 short keychar ;
1525 keychar = short(ev->message & charCodeMask);
1526 keycode = short(ev->message & keyCodeMask) >> 8 ;
1527
1528 wxWindow* focus = wxWindow::FindFocus() ;
1529 if ( focus )
1530 {
1531 long keyval = wxMacTranslateKey(keychar, keycode) ;
1532
1533 wxKeyEvent event(wxEVT_KEY_UP);
1534 event.m_shiftDown = ev->modifiers & shiftKey;
1535 event.m_controlDown = ev->modifiers & controlKey;
1536 event.m_altDown = ev->modifiers & optionKey;
1537 event.m_metaDown = ev->modifiers & cmdKey;
1538 event.m_keyCode = keyval;
1539 event.m_x = ev->where.h;
1540 event.m_y = ev->where.v;
1541 event.m_timeStamp = ev->when;
1542 event.SetEventObject(focus);
1543 bool handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1544 }
1545 }
1546}
1547
1548void wxApp::MacHandleActivateEvent( WXEVENTREF evr )
1549{
1550 EventRecord* ev = (EventRecord*) evr ;
1551 WindowRef window = (WindowRef) ev->message ;
1552 if ( window )
1553 {
1554 bool activate = (ev->modifiers & activeFlag ) ;
1555 WindowClass wclass ;
1556 ::GetWindowClass ( window , &wclass ) ;
1557 if ( wclass == kFloatingWindowClass )
1558 {
1559 // if it is a floater we activate/deactivate the front non-floating window instead
1560 window = ::FrontNonFloatingWindow() ;
1561 }
1562 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1563 if ( win )
1564 win->MacActivate( ev , activate ) ;
1565 }
1566}
1567
1568void wxApp::MacHandleUpdateEvent( WXEVENTREF evr )
1569{
1570 EventRecord* ev = (EventRecord*) evr ;
1571 WindowRef window = (WindowRef) ev->message ;
1572 wxTopLevelWindowMac * win = wxFindWinFromMacWindow( window ) ;
1573 if ( win )
1574 {
1575 if ( !wxPendingDelete.Member(win) )
1576 win->MacUpdate( ev->when ) ;
1577 }
1578 else
1579 {
1580 // since there is no way of telling this foreign window to update itself
1581 // we have to invalidate the update region otherwise we keep getting the same
1582 // event over and over again
1583 BeginUpdate( window ) ;
1584 EndUpdate( window ) ;
1585 }
1586}
1587
1588void wxApp::MacHandleDiskEvent( WXEVENTREF evr )
1589{
1590 EventRecord* ev = (EventRecord*) evr ;
1591 if ( HiWord( ev->message ) != noErr )
1592 {
1593 #if !TARGET_CARBON
1594 OSErr err ;
1595 Point point ;
1596 SetPt( &point , 100 , 100 ) ;
1597
1598 err = DIBadMount( point , ev->message ) ;
1599 wxASSERT( err == noErr ) ;
1600#endif
1601 }
1602}
1603
1604void wxApp::MacHandleOSEvent( WXEVENTREF evr )
1605{
1606 EventRecord* ev = (EventRecord*) evr ;
1607 switch( ( ev->message & osEvtMessageMask ) >> 24 )
1608 {
1609 case suspendResumeMessage :
1610 {
1611 bool isResuming = ev->message & resumeFlag ;
1612#if !TARGET_CARBON
1613 bool convertClipboard = ev->message & convertClipboardFlag ;
1614#else
1615 bool convertClipboard = false;
1616#endif
1617 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
1618 if ( isResuming )
1619 {
1620 WindowRef oldFrontWindow = NULL ;
1621 WindowRef newFrontWindow = NULL ;
1622
1623 // in case we don't take care of activating ourselves, we have to synchronize
1624 // our idea of the active window with the process manager's - which it already activated
1625
1626 if ( !doesActivate )
1627 oldFrontWindow = ::FrontNonFloatingWindow() ;
1628
1629 MacResume( convertClipboard ) ;
1630
1631 newFrontWindow = ::FrontNonFloatingWindow() ;
1632
1633 if ( oldFrontWindow )
1634 {
1635 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( oldFrontWindow ) ;
1636 if ( win )
1637 win->MacActivate( ev , false ) ;
1638 }
1639 if ( newFrontWindow )
1640 {
1641 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( newFrontWindow ) ;
1642 if ( win )
1643 win->MacActivate( ev , true ) ;
1644 }
1645 }
1646 else
1647 {
1648 MacSuspend( convertClipboard ) ;
1649
1650 // in case this suspending did close an active window, another one might
1651 // have surfaced -> lets deactivate that one
1652
1653/* TODO : find out what to do on systems < 10 , perhaps FrontNonFloatingWindow
1654 WindowRef newActiveWindow = ::ActiveNonFloatingWindow() ;
1655 if ( newActiveWindow )
1656 {
1657 wxWindow* win = wxFindWinFromMacWindow( newActiveWindow ) ;
1658 if ( win )
1659 win->MacActivate( ev , false ) ;
1660 }
1661*/
1662 }
1663 }
1664 break ;
1665 case mouseMovedMessage :
1666 {
1667 WindowRef window;
1668
1669 wxWindow* currentMouseWindow = NULL ;
1670
1671 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
1672 &currentMouseWindow ) ;
1673
1674 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
1675 {
1676 wxMouseEvent event ;
1677
1678 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
1679 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
1680
1681 event.m_leftDown = isDown && !controlDown;
1682 event.m_middleDown = FALSE;
1683 event.m_rightDown = isDown && controlDown;
1684 event.m_shiftDown = ev->modifiers & shiftKey;
1685 event.m_controlDown = ev->modifiers & controlKey;
1686 event.m_altDown = ev->modifiers & optionKey;
1687 event.m_metaDown = ev->modifiers & cmdKey;
1688 event.m_x = ev->where.h;
1689 event.m_y = ev->where.v;
1690 event.m_timeStamp = ev->when;
1691 event.SetEventObject(this);
1692
1693 if ( wxWindow::s_lastMouseWindow )
1694 {
1695 wxMouseEvent eventleave(event ) ;
1696 eventleave.SetEventType( wxEVT_LEAVE_WINDOW ) ;
1697 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
1698 }
1699 if ( currentMouseWindow )
1700 {
1701 wxMouseEvent evententer(event ) ;
1702 evententer.SetEventType( wxEVT_ENTER_WINDOW ) ;
1703 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
1704 }
1705 wxWindow::s_lastMouseWindow = currentMouseWindow ;
1706 }
1707
1708 short windowPart = ::FindWindow(ev->where, &window);
1709
1710 switch (windowPart)
1711 {
1712 // fixes for setting the cursor back from dominic mazzoni
1713 case inMenuBar :
1714 UMAShowArrowCursor();
1715 break ;
1716 case inSysWindow :
1717 UMAShowArrowCursor();
1718 break ;
1719 default:
1720 {
1721 // if ( s_lastMouseDown == 0 )
1722 // ev->modifiers |= btnState ;
1723
1724 // Calling GetNextEvent with a zero event mask will always
1725 // pass back a null event. However, it fills the EventRecord
1726 // with the state of the modifier keys. This is needed since
1727 // the modifier state returned by WaitForNextEvent often is
1728 // wrong mouse move events. The attempt above to correct this
1729 // didn't always work (under OS X at least).
1730
1731 EventRecord tmp;
1732 ::GetNextEvent(0, &tmp);
1733 ev->modifiers = tmp.modifiers;
1734
1735 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1736 if ( win )
1737 win->MacMouseMoved( ev , windowPart ) ;
1738 else
1739 UMAShowArrowCursor();
1740
1741 }
1742 break;
1743 }
1744 }
1745 break ;
1746
1747 }
1748}
1749
1750void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
1751{
1752 if (macMenuId == 0)
1753 return; // no menu item selected
1754
1755 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
1756 {
1757 #if ! TARGET_CARBON
1758 Str255 deskAccessoryName ;
1759 GrafPtr savedPort ;
1760
1761 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
1762 GetPort(&savedPort);
1763 OpenDeskAcc(deskAccessoryName);
1764 SetPort(savedPort);
1765 #endif
1766 }
1767 else
1768 {
1769 wxWindow* frontwindow = wxFindWinFromMacWindow( ::FrontWindow() ) ;
1770 if ( frontwindow && wxMenuBar::MacGetInstalledMenuBar() )
1771 wxMenuBar::MacGetInstalledMenuBar()->MacMenuSelect( frontwindow->GetEventHandler() , 0 , macMenuId , macMenuItemNum ) ;
1772 }
1773 HiliteMenu(0);
1774}
1775
1776/*
1777long wxApp::MacTranslateKey(char key, int mods)
1778{
1779}
1780
1781void wxApp::MacAdjustCursor()
1782{
1783}
1784
1785*/
1786/*
1787void
1788wxApp::macAdjustCursor()
1789{
1790 if (ev->what != kHighLevelEvent)
1791 {
1792 wxWindow* theMacWxFrame = wxFrame::MacFindFrameOrDialog(::FrontWindow());
1793 if (theMacWxFrame)
1794 {
1795 if (!theMacWxFrame->MacAdjustCursor(ev->where))
1796 ::SetCursor(&(qd.arrow));
1797 }
1798 }
1799}
1800*/