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