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