]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/app.cpp
fixed to the IFF handler, moved into one file
[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 = inNoWindow ;
1274 if ( wxTheApp->s_captureWindow )
1275 {
1276 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
1277 windowPart = inContent ;
1278 }
1279 else
1280 {
1281 windowPart = ::FindWindow(ev->where, &window) ;
1282 }
1283
1284 switch (windowPart)
1285 {
1286 case inMenuBar :
1287 break ;
1288 case inSysWindow :
1289 break ;
1290 default:
1291 {
1292 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1293 if ( win )
1294 win->MacMouseUp( ev , windowPart ) ;
1295 }
1296 break;
1297 }
1298 }
1299
1300 long wxMacTranslateKey(unsigned char key, unsigned char code) ;
1301 long wxMacTranslateKey(unsigned char key, unsigned char code)
1302 {
1303 long retval = key ;
1304 switch (key)
1305 {
1306 case 0x01 :
1307 retval = WXK_HOME;
1308 break;
1309 case 0x03 :
1310 retval = WXK_RETURN;
1311 break;
1312 case 0x04 :
1313 retval = WXK_END;
1314 break;
1315 case 0x05 :
1316 retval = WXK_HELP;
1317 break;
1318 case 0x08 :
1319 retval = WXK_BACK;
1320 break;
1321 case 0x09 :
1322 retval = WXK_TAB;
1323 break;
1324 case 0x0b :
1325 retval = WXK_PAGEUP;
1326 break;
1327 case 0x0c :
1328 retval = WXK_PAGEDOWN;
1329 break;
1330 case 0x0d :
1331 retval = WXK_RETURN;
1332 break;
1333 case 0x10 :
1334 {
1335 switch( code )
1336 {
1337 case 0x7a :
1338 retval = WXK_F1 ;
1339 break;
1340 case 0x78 :
1341 retval = WXK_F2 ;
1342 break;
1343 case 0x63 :
1344 retval = WXK_F3 ;
1345 break;
1346 case 0x76 :
1347 retval = WXK_F4 ;
1348 break;
1349 case 0x60 :
1350 retval = WXK_F5 ;
1351 break;
1352 case 0x61 :
1353 retval = WXK_F6 ;
1354 break;
1355 case 0x62:
1356 retval = WXK_F7 ;
1357 break;
1358 case 0x64 :
1359 retval = WXK_F8 ;
1360 break;
1361 case 0x65 :
1362 retval = WXK_F9 ;
1363 break;
1364 case 0x6D :
1365 retval = WXK_F10 ;
1366 break;
1367 case 0x67 :
1368 retval = WXK_F11 ;
1369 break;
1370 case 0x6F :
1371 retval = WXK_F12 ;
1372 break;
1373 case 0x69 :
1374 retval = WXK_F13 ;
1375 break;
1376 case 0x6B :
1377 retval = WXK_F14 ;
1378 break;
1379 case 0x71 :
1380 retval = WXK_F15 ;
1381 break;
1382 }
1383 }
1384 break ;
1385 case 0x1b :
1386 retval = WXK_ESCAPE ;
1387 break ;
1388 case 0x1c :
1389 retval = WXK_LEFT ;
1390 break ;
1391 case 0x1d :
1392 retval = WXK_RIGHT ;
1393 break ;
1394 case 0x1e :
1395 retval = WXK_UP ;
1396 break ;
1397 case 0x1f :
1398 retval = WXK_DOWN ;
1399 break ;
1400 case 0x7F :
1401 retval = WXK_DELETE ;
1402 default:
1403 break ;
1404 } // end switch
1405
1406 return retval;
1407 }
1408
1409 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr )
1410 {
1411 EventRecord* ev = (EventRecord*) evr ;
1412 wxToolTip::RemoveToolTips() ;
1413
1414 UInt32 menuresult = UMAMenuEvent(ev) ;
1415 if ( HiWord( menuresult ) )
1416 {
1417 if ( !s_macIsInModalLoop )
1418 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
1419 }
1420 else
1421 {
1422 short keycode ;
1423 short keychar ;
1424 keychar = short(ev->message & charCodeMask);
1425 keycode = short(ev->message & keyCodeMask) >> 8 ;
1426 long keyval = wxMacTranslateKey(keychar, keycode) ;
1427 bool handled = false ;
1428 wxWindow* focus = wxWindow::FindFocus() ;
1429 if ( focus )
1430 {
1431
1432 wxKeyEvent event(wxEVT_KEY_DOWN);
1433 event.m_shiftDown = ev->modifiers & shiftKey;
1434 event.m_controlDown = ev->modifiers & controlKey;
1435 event.m_altDown = ev->modifiers & optionKey;
1436 event.m_metaDown = ev->modifiers & cmdKey;
1437 event.m_keyCode = keyval;
1438 event.m_x = ev->where.h;
1439 event.m_y = ev->where.v;
1440 event.m_timeStamp = ev->when;
1441 event.SetEventObject(focus);
1442 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1443 if ( !handled )
1444 {
1445 #if wxUSE_ACCEL
1446 if (!handled)
1447 {
1448 wxWindow *ancestor = focus;
1449 /*
1450 while (ancestor)
1451 {
1452 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1453 if (command != -1)
1454 {
1455 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1456 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1457 break;
1458 }
1459 if (ancestor->m_isFrame)
1460 break;
1461 ancestor = ancestor->GetParent();
1462 }
1463 */
1464 }
1465 #endif // wxUSE_ACCEL
1466 }
1467 if (!handled)
1468 {
1469 wxKeyEvent event(wxEVT_CHAR);
1470 event.m_shiftDown = ev->modifiers & shiftKey;
1471 event.m_controlDown = ev->modifiers & controlKey;
1472 event.m_altDown = ev->modifiers & optionKey;
1473 event.m_metaDown = ev->modifiers & cmdKey;
1474 event.m_keyCode = keyval;
1475 event.m_x = ev->where.h;
1476 event.m_y = ev->where.v;
1477 event.m_timeStamp = ev->when;
1478 event.SetEventObject(focus);
1479 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1480 }
1481 if ( !handled &&
1482 (keyval == WXK_TAB) &&
1483 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
1484 (focus->GetParent()) &&
1485 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
1486 {
1487 wxNavigationKeyEvent new_event;
1488 new_event.SetEventObject( focus );
1489 new_event.SetDirection( !event.ShiftDown() );
1490 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1491 new_event.SetWindowChange( event.ControlDown() );
1492 new_event.SetCurrentFocus( focus );
1493 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1494 }
1495 }
1496 if ( !handled )
1497 {
1498 // if window is not having a focus still testing for default enter or cancel
1499 // TODO add the UMA version for ActiveNonFloatingWindow
1500 focus = wxFindWinFromMacWindow( FrontWindow() ) ;
1501 if ( focus )
1502 {
1503 if ( keyval == WXK_RETURN )
1504 {
1505 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
1506 wxButton);
1507 if ( def && def->IsEnabled() )
1508 {
1509 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1510 event.SetEventObject(def);
1511 def->Command(event);
1512 return ;
1513 }
1514 }
1515 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1516 else if (keyval == WXK_ESCAPE || (keyval == '.' && ev->modifiers & cmdKey ) )
1517 {
1518 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1519 new_event.SetEventObject( focus );
1520 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1521 }
1522 }
1523 }
1524 }
1525 }
1526
1527 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr )
1528 {
1529 EventRecord* ev = (EventRecord*) evr ;
1530 wxToolTip::RemoveToolTips() ;
1531
1532 UInt32 menuresult = UMAMenuEvent(ev) ;
1533 if ( HiWord( menuresult ) )
1534 {
1535 }
1536 else
1537 {
1538 short keycode ;
1539 short keychar ;
1540 keychar = short(ev->message & charCodeMask);
1541 keycode = short(ev->message & keyCodeMask) >> 8 ;
1542
1543 wxWindow* focus = wxWindow::FindFocus() ;
1544 if ( focus )
1545 {
1546 long keyval = wxMacTranslateKey(keychar, keycode) ;
1547
1548 wxKeyEvent event(wxEVT_KEY_UP);
1549 event.m_shiftDown = ev->modifiers & shiftKey;
1550 event.m_controlDown = ev->modifiers & controlKey;
1551 event.m_altDown = ev->modifiers & optionKey;
1552 event.m_metaDown = ev->modifiers & cmdKey;
1553 event.m_keyCode = keyval;
1554 event.m_x = ev->where.h;
1555 event.m_y = ev->where.v;
1556 event.m_timeStamp = ev->when;
1557 event.SetEventObject(focus);
1558 bool handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1559 }
1560 }
1561 }
1562
1563 void wxApp::MacHandleActivateEvent( WXEVENTREF evr )
1564 {
1565 EventRecord* ev = (EventRecord*) evr ;
1566 WindowRef window = (WindowRef) ev->message ;
1567 if ( window )
1568 {
1569 bool activate = (ev->modifiers & activeFlag ) ;
1570 WindowClass wclass ;
1571 ::GetWindowClass ( window , &wclass ) ;
1572 if ( wclass == kFloatingWindowClass )
1573 {
1574 // if it is a floater we activate/deactivate the front non-floating window instead
1575 window = ::FrontNonFloatingWindow() ;
1576 }
1577 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1578 if ( win )
1579 win->MacActivate( ev , activate ) ;
1580 }
1581 }
1582
1583 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr )
1584 {
1585 EventRecord* ev = (EventRecord*) evr ;
1586 WindowRef window = (WindowRef) ev->message ;
1587 wxTopLevelWindowMac * win = wxFindWinFromMacWindow( window ) ;
1588 if ( win )
1589 {
1590 if ( !wxPendingDelete.Member(win) )
1591 win->MacUpdate( ev->when ) ;
1592 }
1593 else
1594 {
1595 // since there is no way of telling this foreign window to update itself
1596 // we have to invalidate the update region otherwise we keep getting the same
1597 // event over and over again
1598 BeginUpdate( window ) ;
1599 EndUpdate( window ) ;
1600 }
1601 }
1602
1603 void wxApp::MacHandleDiskEvent( WXEVENTREF evr )
1604 {
1605 EventRecord* ev = (EventRecord*) evr ;
1606 if ( HiWord( ev->message ) != noErr )
1607 {
1608 #if !TARGET_CARBON
1609 OSErr err ;
1610 Point point ;
1611 SetPt( &point , 100 , 100 ) ;
1612
1613 err = DIBadMount( point , ev->message ) ;
1614 wxASSERT( err == noErr ) ;
1615 #endif
1616 }
1617 }
1618
1619 void wxApp::MacHandleOSEvent( WXEVENTREF evr )
1620 {
1621 EventRecord* ev = (EventRecord*) evr ;
1622 switch( ( ev->message & osEvtMessageMask ) >> 24 )
1623 {
1624 case suspendResumeMessage :
1625 {
1626 bool isResuming = ev->message & resumeFlag ;
1627 #if !TARGET_CARBON
1628 bool convertClipboard = ev->message & convertClipboardFlag ;
1629 #else
1630 bool convertClipboard = false;
1631 #endif
1632 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
1633 if ( isResuming )
1634 {
1635 WindowRef oldFrontWindow = NULL ;
1636 WindowRef newFrontWindow = NULL ;
1637
1638 // in case we don't take care of activating ourselves, we have to synchronize
1639 // our idea of the active window with the process manager's - which it already activated
1640
1641 if ( !doesActivate )
1642 oldFrontWindow = ::FrontNonFloatingWindow() ;
1643
1644 MacResume( convertClipboard ) ;
1645
1646 newFrontWindow = ::FrontNonFloatingWindow() ;
1647
1648 if ( oldFrontWindow )
1649 {
1650 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( oldFrontWindow ) ;
1651 if ( win )
1652 win->MacActivate( ev , false ) ;
1653 }
1654 if ( newFrontWindow )
1655 {
1656 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( newFrontWindow ) ;
1657 if ( win )
1658 win->MacActivate( ev , true ) ;
1659 }
1660 }
1661 else
1662 {
1663 MacSuspend( convertClipboard ) ;
1664
1665 // in case this suspending did close an active window, another one might
1666 // have surfaced -> lets deactivate that one
1667
1668 /* TODO : find out what to do on systems < 10 , perhaps FrontNonFloatingWindow
1669 WindowRef newActiveWindow = ::ActiveNonFloatingWindow() ;
1670 if ( newActiveWindow )
1671 {
1672 wxWindow* win = wxFindWinFromMacWindow( newActiveWindow ) ;
1673 if ( win )
1674 win->MacActivate( ev , false ) ;
1675 }
1676 */
1677 }
1678 }
1679 break ;
1680 case mouseMovedMessage :
1681 {
1682 WindowRef window;
1683
1684 wxWindow* currentMouseWindow = NULL ;
1685
1686 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
1687 &currentMouseWindow ) ;
1688 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
1689 {
1690 wxMouseEvent event ;
1691
1692 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
1693 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
1694
1695 event.m_leftDown = isDown && !controlDown;
1696 event.m_middleDown = FALSE;
1697 event.m_rightDown = isDown && controlDown;
1698 event.m_shiftDown = ev->modifiers & shiftKey;
1699 event.m_controlDown = ev->modifiers & controlKey;
1700 event.m_altDown = ev->modifiers & optionKey;
1701 event.m_metaDown = ev->modifiers & cmdKey;
1702 event.m_x = ev->where.h;
1703 event.m_y = ev->where.v;
1704 event.m_timeStamp = ev->when;
1705 event.SetEventObject(this);
1706
1707 if ( wxWindow::s_lastMouseWindow )
1708 {
1709 wxMouseEvent eventleave(event);
1710 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
1711 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
1712
1713 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
1714 }
1715 if ( currentMouseWindow )
1716 {
1717 wxMouseEvent evententer(event);
1718 evententer.SetEventType( wxEVT_ENTER_WINDOW );
1719 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
1720
1721 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
1722 }
1723 wxWindow::s_lastMouseWindow = currentMouseWindow ;
1724 }
1725
1726 short windowPart = ::FindWindow(ev->where, &window);
1727
1728 switch (windowPart)
1729 {
1730 // fixes for setting the cursor back from dominic mazzoni
1731 case inMenuBar :
1732 UMAShowArrowCursor();
1733 break ;
1734 case inSysWindow :
1735 UMAShowArrowCursor();
1736 break ;
1737 default:
1738 {
1739 // if ( s_lastMouseDown == 0 )
1740 // ev->modifiers |= btnState ;
1741
1742 // Calling GetNextEvent with a zero event mask will always
1743 // pass back a null event. However, it fills the EventRecord
1744 // with the state of the modifier keys. This is needed since
1745 // the modifier state returned by WaitForNextEvent often is
1746 // wrong mouse move events. The attempt above to correct this
1747 // didn't always work (under OS X at least).
1748
1749 EventRecord tmp;
1750 ::GetNextEvent(0, &tmp);
1751 ev->modifiers = tmp.modifiers;
1752
1753 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1754 if ( win )
1755 win->MacMouseMoved( ev , windowPart ) ;
1756 else
1757 UMAShowArrowCursor();
1758
1759 }
1760 break;
1761 }
1762 }
1763 break ;
1764
1765 }
1766 }
1767
1768 void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
1769 {
1770 if (macMenuId == 0)
1771 return; // no menu item selected
1772
1773 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
1774 {
1775 #if ! TARGET_CARBON
1776 Str255 deskAccessoryName ;
1777 GrafPtr savedPort ;
1778
1779 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
1780 GetPort(&savedPort);
1781 OpenDeskAcc(deskAccessoryName);
1782 SetPort(savedPort);
1783 #endif
1784 }
1785 else
1786 {
1787 wxWindow* frontwindow = wxFindWinFromMacWindow( ::FrontWindow() ) ;
1788 if ( frontwindow && wxMenuBar::MacGetInstalledMenuBar() )
1789 wxMenuBar::MacGetInstalledMenuBar()->MacMenuSelect( frontwindow->GetEventHandler() , 0 , macMenuId , macMenuItemNum ) ;
1790 }
1791 HiliteMenu(0);
1792 }
1793
1794 /*
1795 long wxApp::MacTranslateKey(char key, int mods)
1796 {
1797 }
1798
1799 void wxApp::MacAdjustCursor()
1800 {
1801 }
1802
1803 */
1804 /*
1805 void
1806 wxApp::macAdjustCursor()
1807 {
1808 if (ev->what != kHighLevelEvent)
1809 {
1810 wxWindow* theMacWxFrame = wxFrame::MacFindFrameOrDialog(::FrontWindow());
1811 if (theMacWxFrame)
1812 {
1813 if (!theMacWxFrame->MacAdjustCursor(ev->where))
1814 ::SetCursor(&(qd.arrow));
1815 }
1816 }
1817 }
1818 */