]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/app.cpp
some wxArtProv docs
[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/defs.h"
17
18 #include "wx/window.h"
19 #include "wx/frame.h"
20 #include "wx/button.h"
21 #include "wx/app.h"
22 #include "wx/utils.h"
23 #include "wx/gdicmn.h"
24 #include "wx/pen.h"
25 #include "wx/brush.h"
26 #include "wx/cursor.h"
27 #include "wx/intl.h"
28 #include "wx/icon.h"
29 #include "wx/palette.h"
30 #include "wx/dc.h"
31 #include "wx/dialog.h"
32 #include "wx/msgdlg.h"
33 #include "wx/log.h"
34 #include "wx/module.h"
35 #include "wx/memory.h"
36 #include "wx/tooltip.h"
37 #include "wx/textctrl.h"
38 #include "wx/menu.h"
39 #if wxUSE_WX_RESOURCES
40 # include "wx/resource.h"
41 #endif
42
43 #include <string.h>
44
45 // mac
46
47 #ifndef __DARWIN__
48 #if __option(profile)
49 #include <profiler.h>
50 #endif
51 #endif
52
53 #include "apprsrc.h"
54
55 #include "wx/mac/uma.h"
56 #include "wx/mac/macnotfy.h"
57
58 #ifdef __DARWIN__
59 # include <CoreServices/CoreServices.h>
60 #else
61 # include <Sound.h>
62 # include <Threads.h>
63 # include <ToolUtils.h>
64 # include <DiskInit.h>
65 # include <Devices.h>
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 // GD: UNUSED 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 // GD: UNUSED 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 // GD: UNUSED 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 // GD: UNUSED 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 ( unsigned 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 #ifdef WXMAKINGDLL
377 #ifndef __DARWIN__
378 // we know it's there ;-)
379 WXIMPORT char std::__throws_bad_alloc ;
380 #endif
381 #endif
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 < 0x0860 )
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 wxStAppResource resload ;
458 short itemHit;
459 Str255 message;
460
461 GetIndString(message, 128, error);
462 UMAShowArrowCursor() ;
463 ParamText("\pFatal Error", message, (ConstStr255Param)"\p", (ConstStr255Param)"\p");
464 itemHit = Alert(128, nil);
465 return FALSE ;
466 }
467
468 #ifndef __DARWIN__
469 #if __option(profile)
470 ProfilerInit( collectDetailed, bestTimeBase , 20000 , 40 ) ;
471 #endif
472 #endif
473
474 // now avoid exceptions thrown for new (bad_alloc)
475
476 #ifndef __DARWIN__
477 std::__throws_bad_alloc = FALSE ;
478 #endif
479
480 s_macCursorRgn = ::NewRgn() ;
481
482 #ifdef __WXMSW__
483 wxBuffer = new char[1500];
484 #else
485 wxBuffer = new char[BUFSIZ + 512];
486 #endif
487
488 wxClassInfo::InitializeClasses();
489
490 #if wxUSE_RESOURCES
491 // wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
492 #endif
493
494 #if wxUSE_THREADS
495 wxPendingEventsLocker = new wxCriticalSection;
496 #endif
497 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
498 wxTheColourDatabase->Initialize();
499
500 #ifdef __WXDEBUG__
501 #if wxUSE_LOG
502 // flush the logged messages if any and install a 'safer' log target: the
503 // default one (wxLogGui) can't be used after the resources are freed just
504 // below and the user suppliedo ne might be even more unsafe (using any
505 // wxWindows GUI function is unsafe starting from now)
506 wxLog::DontCreateOnDemand();
507
508 // this will flush the old messages if any
509 delete wxLog::SetActiveTarget(new wxLogStderr);
510 #endif // wxUSE_LOG
511 #endif
512
513 wxWinMacWindowList = new wxList(wxKEY_INTEGER);
514 wxWinMacControlList = new wxList(wxKEY_INTEGER);
515
516 wxInitializeStockLists();
517 wxInitializeStockObjects();
518
519 #if wxUSE_WX_RESOURCES
520 wxInitializeResourceSystem();
521 #endif
522
523 wxBitmap::InitStandardHandlers();
524
525 wxModule::RegisterModules();
526 if (!wxModule::InitializeModules()) {
527 return FALSE;
528 }
529
530 wxMacCreateNotifierTable() ;
531
532 UMAShowArrowCursor() ;
533
534 return TRUE;
535 }
536
537 void wxApp::CleanUp()
538 {
539 wxToolTip::RemoveToolTips() ;
540 #if wxUSE_LOG
541 // flush the logged messages if any and install a 'safer' log target: the
542 // default one (wxLogGui) can't be used after the resources are freed just
543 // below and the user suppliedo ne might be even more unsafe (using any
544 // wxWindows GUI function is unsafe starting from now)
545 wxLog::DontCreateOnDemand();
546
547 // this will flush the old messages if any
548 delete wxLog::SetActiveTarget(new wxLogStderr);
549 #endif // wxUSE_LOG
550
551 // One last chance for pending objects to be cleaned up
552 wxTheApp->DeletePendingObjects();
553
554 wxModule::CleanUpModules();
555
556 #if wxUSE_WX_RESOURCES
557 wxCleanUpResourceSystem();
558 #endif
559
560 wxDeleteStockObjects() ;
561
562 // Destroy all GDI lists, etc.
563 wxDeleteStockLists();
564
565 delete wxTheColourDatabase;
566 wxTheColourDatabase = NULL;
567
568 wxBitmap::CleanUpHandlers();
569
570 delete[] wxBuffer;
571 wxBuffer = NULL;
572
573 wxMacDestroyNotifierTable() ;
574 if (wxWinMacWindowList)
575 delete wxWinMacWindowList ;
576
577 if (wxWinMacControlList)
578 delete wxWinMacControlList ;
579
580 delete wxPendingEvents;
581 #if wxUSE_THREADS
582 delete wxPendingEventsLocker;
583 // If we don't do the following, we get an apparent memory leak.
584 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
585 #endif
586
587 wxClassInfo::CleanUpClasses();
588
589 #ifndef __DARWIN__
590 #if __option(profile)
591 ProfilerDump( "\papp.prof" ) ;
592 ProfilerTerm() ;
593 #endif
594 #endif
595
596 delete wxTheApp;
597 wxTheApp = NULL;
598
599 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
600 // At this point we want to check if there are any memory
601 // blocks that aren't part of the wxDebugContext itself,
602 // as a special case. Then when dumping we need to ignore
603 // wxDebugContext, too.
604 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
605 {
606 wxLogDebug(wxT("There were memory leaks."));
607 wxDebugContext::Dump();
608 wxDebugContext::PrintStatistics();
609 }
610 // wxDebugContext::SetStream(NULL, NULL);
611 #endif
612
613 #if wxUSE_LOG
614 // do it as the very last thing because everything else can log messages
615 delete wxLog::SetActiveTarget(NULL);
616 #endif // wxUSE_LOG
617
618 UMACleanupToolbox() ;
619 if (s_macCursorRgn)
620 ::DisposeRgn((RgnHandle)s_macCursorRgn);
621
622 #if 0
623 TerminateAE() ;
624 #endif
625 }
626
627 //----------------------------------------------------------------------
628 // wxEntry
629 //----------------------------------------------------------------------
630
631 short gCurrentResource = -1 ;
632
633 wxStAppResource::wxStAppResource()
634 {
635 m_currentRefNum = CurResFile() ;
636 if ( gCurrentResource != -1 )
637 {
638 UseResFile( gCurrentResource ) ;
639 }
640 }
641
642 wxStAppResource::~wxStAppResource()
643 {
644 if ( m_currentRefNum != -1 )
645 {
646 UseResFile( m_currentRefNum ) ;
647 }
648 }
649
650 #ifdef WXMAKINGDLL
651
652 // for shared libraries we have to manually get the correct resource ref num upon
653 // initializing and releasing when terminating, therefore the __wxinitialize and __wxterminate
654 // must be used
655
656 #ifdef __cplusplus
657 extern "C" {
658 #endif
659
660 void __sinit(void); /* (generated by linker) */
661 pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
662 pascal void __terminate(void);
663
664 #ifdef __cplusplus
665 }
666 #endif
667
668 pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock)
669 {
670 gCurrentResource = -1;
671
672 if (theInitBlock->fragLocator.where == kDataForkCFragLocator) {
673 gCurrentResource =
674 FSpOpenResFile(theInitBlock->fragLocator.u.onDisk.fileSpec,
675 fsRdPerm);
676 }
677 return __initialize( theInitBlock ) ;
678 }
679
680 pascal void __wxterminate(void)
681 {
682 if (gCurrentResource != -1)
683 CloseResFile(gCurrentResource);
684 __terminate() ;
685 }
686 #endif
687
688 int WXDLLEXPORT wxEntryStart( int argc, char *argv[] )
689 {
690 return wxApp::Initialize();
691 }
692
693
694 int WXDLLEXPORT wxEntryInitGui()
695 {
696 return wxTheApp->OnInitGui();
697 }
698
699
700 void WXDLLEXPORT wxEntryCleanup()
701 {
702 wxApp::CleanUp();
703 }
704
705
706 int wxEntry( int argc, char *argv[] , bool enterLoop )
707 {
708 gCurrentResource = CurResFile() ;
709 #ifdef __MWERKS__
710 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
711 // This seems to be necessary since there are 'rogue'
712 // objects present at this point (perhaps global objects?)
713 // Setting a checkpoint will ignore them as far as the
714 // memory checking facility is concerned.
715 // Of course you may argue that memory allocated in globals should be
716 // checked, but this is a reasonable compromise.
717 wxDebugContext::SetCheckpoint();
718 #endif
719 #endif
720 if (!wxEntryStart(argc, argv)) {
721 return 0;
722 }
723 // create the application object or ensure that one already exists
724 if (!wxTheApp)
725 {
726 // The app may have declared a global application object, but we recommend
727 // the IMPLEMENT_APP macro is used instead, which sets an initializer
728 // function for delayed, dynamic app object construction.
729 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
730 wxT("No initializer - use IMPLEMENT_APP macro.") );
731
732 wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) ();
733 }
734
735 wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
736
737 #ifndef __DARWIN__
738 argc = 0 ; // currently we don't support files as parameters
739 #endif
740 // we could try to get the open apple events here to adjust argc and argv better
741
742 wxTheApp->argc = argc;
743 wxTheApp->argv = argv;
744
745 // GUI-specific initialization, such as creating an app context.
746 wxEntryInitGui();
747
748
749 // Here frames insert themselves automatically
750 // into wxTopLevelWindows by getting created
751 // in OnInit().
752
753 int retValue = 0;
754
755 if ( wxTheApp->OnInit() )
756 {
757 if ( enterLoop )
758 {
759 retValue = wxTheApp->OnRun();
760 }
761 else
762 // We want to initialize, but not run or exit immediately.
763 return 1;
764 }
765 //else: app initialization failed, so we skipped OnRun()
766
767 wxWindow *topWindow = wxTheApp->GetTopWindow();
768 if ( topWindow )
769 {
770 // Forcibly delete the window.
771 if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
772 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
773 {
774 topWindow->Close(TRUE);
775 wxTheApp->DeletePendingObjects();
776 }
777 else
778 {
779 delete topWindow;
780 wxTheApp->SetTopWindow(NULL);
781 }
782 }
783
784 wxTheApp->OnExit();
785
786 wxEntryCleanup();
787
788 return retValue;
789 }
790
791 // Static member initialization
792 wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
793
794 wxApp::wxApp()
795 {
796 m_topWindow = NULL;
797 wxTheApp = this;
798
799 #if WXWIN_COMPATIBILITY_2_2
800 m_wantDebugOutput = TRUE;
801 #endif
802
803 argc = 0;
804 argv = NULL;
805
806 m_printMode = wxPRINT_WINDOWS;
807 m_exitOnFrameDelete = TRUE;
808 m_auto3D = TRUE;
809 }
810
811 bool wxApp::Initialized()
812 {
813 if (GetTopWindow())
814 return TRUE;
815 else
816 return FALSE;
817 }
818
819 int wxApp::MainLoop()
820 {
821 m_keepGoing = TRUE;
822
823 while (m_keepGoing)
824 {
825 MacDoOneEvent() ;
826 }
827
828 return 0;
829 }
830
831 // Returns TRUE if more time is needed.
832 bool wxApp::ProcessIdle()
833 {
834 wxIdleEvent event;
835 event.SetEventObject(this);
836 ProcessEvent(event);
837
838 return event.MoreRequested();
839 }
840
841 void wxApp::ExitMainLoop()
842 {
843 m_keepGoing = FALSE;
844 }
845
846 // Is a message/event pending?
847 bool wxApp::Pending()
848 {
849 EventRecord event ;
850
851 return EventAvail( everyEvent , &event ) ;
852 }
853
854 // Dispatch a message.
855 void wxApp::Dispatch()
856 {
857 MacDoOneEvent() ;
858 }
859
860 void wxApp::OnIdle(wxIdleEvent& event)
861 {
862 static bool s_inOnIdle = FALSE;
863
864 // Avoid recursion (via ProcessEvent default case)
865 if ( s_inOnIdle )
866 return;
867
868
869 s_inOnIdle = TRUE;
870
871 // 'Garbage' collection of windows deleted with Close().
872 DeletePendingObjects();
873
874 // flush the logged messages if any
875 wxLog *pLog = wxLog::GetActiveTarget();
876 if ( pLog != NULL && pLog->HasPendingMessages() )
877 pLog->Flush();
878
879 // Send OnIdle events to all windows
880 bool needMore = SendIdleEvents();
881
882 if (needMore)
883 event.RequestMore(TRUE);
884
885 // If they are pending events, we must process them: pending events are
886 // either events to the threads other than main or events posted with
887 // wxPostEvent() functions
888 wxMacProcessNotifierAndPendingEvents();
889
890 s_inOnIdle = FALSE;
891 }
892
893 void wxWakeUpIdle()
894 {
895 wxMacWakeUp() ;
896 }
897
898 // Send idle event to all top-level windows
899 bool wxApp::SendIdleEvents()
900 {
901 bool needMore = FALSE;
902 wxNode* node = wxTopLevelWindows.First();
903 while (node)
904 {
905 wxWindow* win = (wxWindow*) node->Data();
906 if (SendIdleEvents(win))
907 needMore = TRUE;
908
909 node = node->Next();
910 }
911 return needMore;
912 }
913
914 // Send idle event to window and all subwindows
915 bool wxApp::SendIdleEvents(wxWindow* win)
916 {
917 bool needMore = FALSE;
918
919 wxIdleEvent event;
920 event.SetEventObject(win);
921 win->ProcessEvent(event);
922
923 if (event.MoreRequested())
924 needMore = TRUE;
925
926 wxNode* node = win->GetChildren().First();
927 while (node)
928 {
929 wxWindow* win = (wxWindow*) node->Data();
930 if (SendIdleEvents(win))
931 needMore = TRUE;
932
933 node = node->Next();
934 }
935 return needMore ;
936 }
937
938 void wxApp::DeletePendingObjects()
939 {
940 wxNode *node = wxPendingDelete.First();
941 while (node)
942 {
943 wxObject *obj = (wxObject *)node->Data();
944
945 delete obj;
946
947 if (wxPendingDelete.Member(obj))
948 delete node;
949
950 // Deleting one object may have deleted other pending
951 // objects, so start from beginning of list again.
952 node = wxPendingDelete.First();
953 }
954 }
955
956 void wxExit()
957 {
958 wxLogError(_("Fatal error: exiting"));
959
960 wxApp::CleanUp();
961 ::ExitToShell() ;
962 }
963
964 void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
965 {
966 if (GetTopWindow())
967 GetTopWindow()->Close(TRUE);
968 }
969
970 // Default behaviour: close the application with prompts. The
971 // user can veto the close, and therefore the end session.
972 void wxApp::OnQueryEndSession(wxCloseEvent& event)
973 {
974 if (GetTopWindow())
975 {
976 if (!GetTopWindow()->Close(!event.CanVeto()))
977 event.Veto(TRUE);
978 }
979 }
980
981 extern "C" void wxCYield() ;
982 void wxCYield()
983 {
984 wxYield() ;
985 }
986
987 // Yield to other processes
988
989 bool wxApp::Yield(bool onlyIfNeeded)
990 {
991 static bool s_inYield = FALSE;
992
993 if (s_inYield)
994 {
995 if ( !onlyIfNeeded )
996 {
997 wxFAIL_MSG( wxT("wxYield called recursively" ) );
998 }
999
1000 return FALSE;
1001 }
1002
1003 s_inYield = TRUE;
1004
1005 #if wxUSE_THREADS
1006 YieldToAnyThread() ;
1007 #endif
1008 EventRecord event ;
1009
1010 long sleepTime = 1 ; //::GetCaretTime();
1011
1012 while ( !wxTheApp->IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, (RgnHandle) wxApp::s_macCursorRgn))
1013 {
1014 wxTheApp->MacHandleOneEvent( &event );
1015 if ( event.what != kHighLevelEvent )
1016 SetRectRgn( (RgnHandle) wxApp::s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
1017 }
1018
1019 wxMacProcessNotifierAndPendingEvents() ;
1020
1021 s_inYield = FALSE;
1022
1023 return TRUE;
1024 }
1025
1026 // platform specifics
1027
1028 void wxApp::MacSuspend( bool convertClipboard )
1029 {
1030 // we have to deactive the top level windows manually
1031
1032 wxNode* node = wxTopLevelWindows.First();
1033 while (node)
1034 {
1035 wxTopLevelWindow* win = (wxTopLevelWindow*) node->Data();
1036 win->MacActivate( MacGetCurrentEvent() , false ) ;
1037
1038 node = node->Next();
1039 }
1040
1041 s_lastMouseDown = 0 ;
1042 if( convertClipboard )
1043 {
1044 MacConvertPrivateToPublicScrap() ;
1045 }
1046
1047 ::HideFloatingWindows() ;
1048 }
1049
1050 void wxApp::MacResume( bool convertClipboard )
1051 {
1052 s_lastMouseDown = 0 ;
1053 if( convertClipboard )
1054 {
1055 MacConvertPublicToPrivateScrap() ;
1056 }
1057
1058 ::ShowFloatingWindows() ;
1059 }
1060
1061 void wxApp::MacConvertPrivateToPublicScrap()
1062 {
1063 }
1064
1065 void wxApp::MacConvertPublicToPrivateScrap()
1066 {
1067 }
1068
1069 void wxApp::MacDoOneEvent()
1070 {
1071 EventRecord event ;
1072
1073 long sleepTime = 1; // GetCaretTime() / 4 ;
1074
1075 if (WaitNextEvent(everyEvent, &event, sleepTime, (RgnHandle) s_macCursorRgn))
1076 {
1077 MacHandleOneEvent( &event );
1078 }
1079 else
1080 {
1081 // idlers
1082 WindowPtr window = ::FrontWindow() ;
1083 if ( window )
1084 ::IdleControls( window ) ;
1085
1086 wxTheApp->ProcessIdle() ;
1087 }
1088 if ( event.what != kHighLevelEvent )
1089 SetRectRgn( (RgnHandle) s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
1090
1091 // repeaters
1092
1093 DeletePendingObjects() ;
1094 wxMacProcessNotifierAndPendingEvents() ;
1095 }
1096
1097 void wxApp::MacHandleOneEvent( WXEVENTREF evr )
1098 {
1099 EventRecord* ev = (EventRecord*) evr ;
1100 m_macCurrentEvent = ev ;
1101
1102 wxApp::sm_lastMessageTime = ev->when ;
1103
1104 switch (ev->what)
1105 {
1106 case mouseDown:
1107 MacHandleMouseDownEvent( ev ) ;
1108 if ( ev->modifiers & controlKey )
1109 s_lastMouseDown = 2;
1110 else
1111 s_lastMouseDown = 1;
1112 break;
1113 case mouseUp:
1114 if ( s_lastMouseDown == 2 )
1115 {
1116 ev->modifiers |= controlKey ;
1117 }
1118 else
1119 {
1120 ev->modifiers &= ~controlKey ;
1121 }
1122 MacHandleMouseUpEvent( ev ) ;
1123 s_lastMouseDown = 0;
1124 break;
1125 case activateEvt:
1126 MacHandleActivateEvent( ev ) ;
1127 break;
1128 case updateEvt:
1129 MacHandleUpdateEvent( ev ) ;
1130 break;
1131 case keyDown:
1132 case autoKey:
1133 MacHandleKeyDownEvent( ev ) ;
1134 break;
1135 case keyUp:
1136 MacHandleKeyUpEvent( ev ) ;
1137 break;
1138 case diskEvt:
1139 MacHandleDiskEvent( ev ) ;
1140 break;
1141 case osEvt:
1142 MacHandleOSEvent( ev ) ;
1143 break;
1144 case kHighLevelEvent:
1145 MacHandleHighLevelEvent( ev ) ;
1146 break;
1147 default:
1148 break;
1149 }
1150 wxMacProcessNotifierAndPendingEvents() ;
1151 }
1152
1153 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr )
1154 {
1155 EventRecord* ev = (EventRecord*) evr ;
1156 ::AEProcessAppleEvent( ev ) ;
1157 }
1158
1159 bool s_macIsInModalLoop = false ;
1160
1161 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr )
1162 {
1163 EventRecord* ev = (EventRecord*) evr ;
1164 wxToolTip::RemoveToolTips() ;
1165
1166 WindowRef window;
1167 WindowRef frontWindow = ::FrontNonFloatingWindow() ;
1168 WindowAttributes frontWindowAttributes = NULL ;
1169 if ( frontWindow )
1170 ::GetWindowAttributes( frontWindow , &frontWindowAttributes ) ;
1171
1172 short windowPart = ::FindWindow(ev->where, &window);
1173 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1174 if ( wxPendingDelete.Member(win) )
1175 return ;
1176
1177 BitMap screenBits;
1178 GetQDGlobalsScreenBits( &screenBits );
1179
1180 switch (windowPart)
1181 {
1182 case inMenuBar :
1183 if ( s_macIsInModalLoop )
1184 {
1185 SysBeep ( 30 ) ;
1186 }
1187 else
1188 {
1189 UInt32 menuresult = MenuSelect(ev->where) ;
1190 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) );
1191 s_lastMouseDown = 0;
1192 }
1193 break ;
1194 #if !TARGET_CARBON
1195 case inSysWindow :
1196 SystemClick( ev , window ) ;
1197 s_lastMouseDown = 0;
1198 break ;
1199 #endif
1200 case inDrag :
1201 if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) )
1202 {
1203 SysBeep ( 30 ) ;
1204 }
1205 else
1206 {
1207 DragWindow(window, ev->where, &screenBits.bounds);
1208 if (win)
1209 {
1210 GrafPtr port ;
1211 GetPort( &port ) ;
1212 Point pt = { 0, 0 } ;
1213 SetPortWindowPort(window) ;
1214 LocalToGlobal( &pt ) ;
1215 SetPort( port ) ;
1216 win->SetSize( pt.h , pt.v , -1 ,
1217 -1 , wxSIZE_USE_EXISTING);
1218 }
1219 s_lastMouseDown = 0;
1220 }
1221 break ;
1222 case inGoAway:
1223 if (TrackGoAway(window, ev->where))
1224 {
1225 if ( win )
1226 win->Close() ;
1227 }
1228 s_lastMouseDown = 0;
1229 break;
1230 case inGrow:
1231 {
1232 int growResult = GrowWindow(window , ev->where, &screenBits.bounds);
1233 if (growResult != 0)
1234 {
1235 int newWidth = LoWord(growResult);
1236 int newHeight = HiWord(growResult);
1237 int oldWidth, oldHeight;
1238
1239
1240 if (win)
1241 {
1242 win->GetSize(&oldWidth, &oldHeight);
1243 if (newWidth == 0)
1244 newWidth = oldWidth;
1245 if (newHeight == 0)
1246 newHeight = oldHeight;
1247 win->SetSize( -1, -1, newWidth, newHeight, wxSIZE_USE_EXISTING);
1248 }
1249 }
1250 s_lastMouseDown = 0;
1251 }
1252 break;
1253 case inZoomIn:
1254 case inZoomOut:
1255 if (TrackBox(window, ev->where, windowPart))
1256 {
1257 // TODO setup size event
1258 ZoomWindow( window , windowPart , false ) ;
1259 if (win)
1260 {
1261 Rect tempRect ;
1262
1263 GetWindowPortBounds(window, &tempRect ) ;
1264 win->SetSize( -1, -1, tempRect.right-tempRect.left ,
1265 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
1266 }
1267 }
1268 s_lastMouseDown = 0;
1269 break;
1270 case inCollapseBox :
1271 // TODO setup size event
1272 s_lastMouseDown = 0;
1273 break ;
1274
1275 case inContent :
1276 {
1277 GrafPtr port ;
1278 GetPort( &port ) ;
1279 SetPortWindowPort(window) ;
1280 SetPort( port ) ;
1281 }
1282 if ( window != frontWindow && wxTheApp->s_captureWindow == NULL )
1283 {
1284 if ( s_macIsInModalLoop )
1285 {
1286 SysBeep ( 30 ) ;
1287 }
1288 else if ( UMAIsWindowFloating( window ) )
1289 {
1290 if ( win )
1291 win->MacMouseDown( ev , windowPart ) ;
1292 }
1293 else
1294 {
1295 if ( win )
1296 win->MacMouseDown( ev , windowPart ) ;
1297 ::SelectWindow( window ) ;
1298 }
1299 }
1300 else
1301 {
1302 if ( win )
1303 win->MacMouseDown( ev , windowPart ) ;
1304 }
1305 break ;
1306
1307 default:
1308 break;
1309 }
1310 }
1311
1312 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr )
1313 {
1314 EventRecord* ev = (EventRecord*) evr ;
1315 WindowRef window;
1316
1317 short windowPart = inNoWindow ;
1318 if ( wxTheApp->s_captureWindow )
1319 {
1320 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
1321 windowPart = inContent ;
1322 }
1323 else
1324 {
1325 windowPart = ::FindWindow(ev->where, &window) ;
1326 }
1327
1328 switch (windowPart)
1329 {
1330 case inMenuBar :
1331 break ;
1332 case inSysWindow :
1333 break ;
1334 default:
1335 {
1336 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1337 if ( win )
1338 win->MacMouseUp( ev , windowPart ) ;
1339 }
1340 break;
1341 }
1342 }
1343
1344 long wxMacTranslateKey(unsigned char key, unsigned char code) ;
1345 long wxMacTranslateKey(unsigned char key, unsigned char code)
1346 {
1347 long retval = key ;
1348 switch (key)
1349 {
1350 case 0x01 :
1351 retval = WXK_HOME;
1352 break;
1353 case 0x03 :
1354 retval = WXK_RETURN;
1355 break;
1356 case 0x04 :
1357 retval = WXK_END;
1358 break;
1359 case 0x05 :
1360 retval = WXK_HELP;
1361 break;
1362 case 0x08 :
1363 retval = WXK_BACK;
1364 break;
1365 case 0x09 :
1366 retval = WXK_TAB;
1367 break;
1368 case 0x0b :
1369 retval = WXK_PAGEUP;
1370 break;
1371 case 0x0c :
1372 retval = WXK_PAGEDOWN;
1373 break;
1374 case 0x0d :
1375 retval = WXK_RETURN;
1376 break;
1377 case 0x10 :
1378 {
1379 switch( code )
1380 {
1381 case 0x7a :
1382 retval = WXK_F1 ;
1383 break;
1384 case 0x78 :
1385 retval = WXK_F2 ;
1386 break;
1387 case 0x63 :
1388 retval = WXK_F3 ;
1389 break;
1390 case 0x76 :
1391 retval = WXK_F4 ;
1392 break;
1393 case 0x60 :
1394 retval = WXK_F5 ;
1395 break;
1396 case 0x61 :
1397 retval = WXK_F6 ;
1398 break;
1399 case 0x62:
1400 retval = WXK_F7 ;
1401 break;
1402 case 0x64 :
1403 retval = WXK_F8 ;
1404 break;
1405 case 0x65 :
1406 retval = WXK_F9 ;
1407 break;
1408 case 0x6D :
1409 retval = WXK_F10 ;
1410 break;
1411 case 0x67 :
1412 retval = WXK_F11 ;
1413 break;
1414 case 0x6F :
1415 retval = WXK_F12 ;
1416 break;
1417 case 0x69 :
1418 retval = WXK_F13 ;
1419 break;
1420 case 0x6B :
1421 retval = WXK_F14 ;
1422 break;
1423 case 0x71 :
1424 retval = WXK_F15 ;
1425 break;
1426 }
1427 }
1428 break ;
1429 case 0x1b :
1430 retval = WXK_ESCAPE ;
1431 break ;
1432 case 0x1c :
1433 retval = WXK_LEFT ;
1434 break ;
1435 case 0x1d :
1436 retval = WXK_RIGHT ;
1437 break ;
1438 case 0x1e :
1439 retval = WXK_UP ;
1440 break ;
1441 case 0x1f :
1442 retval = WXK_DOWN ;
1443 break ;
1444 case 0x7F :
1445 retval = WXK_DELETE ;
1446 default:
1447 break ;
1448 } // end switch
1449
1450 return retval;
1451 }
1452
1453 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr )
1454 {
1455 EventRecord* ev = (EventRecord*) evr ;
1456 wxToolTip::RemoveToolTips() ;
1457
1458 UInt32 menuresult = UMAMenuEvent(ev) ;
1459 if ( HiWord( menuresult ) )
1460 {
1461 if ( !s_macIsInModalLoop )
1462 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
1463 }
1464 else
1465 {
1466 short keycode ;
1467 short keychar ;
1468 keychar = short(ev->message & charCodeMask);
1469 keycode = short(ev->message & keyCodeMask) >> 8 ;
1470 long keyval = wxMacTranslateKey(keychar, keycode) ;
1471 bool handled = false ;
1472 wxWindow* focus = wxWindow::FindFocus() ;
1473 if ( focus )
1474 {
1475
1476 wxKeyEvent event(wxEVT_KEY_DOWN);
1477 event.m_shiftDown = ev->modifiers & shiftKey;
1478 event.m_controlDown = ev->modifiers & controlKey;
1479 event.m_altDown = ev->modifiers & optionKey;
1480 event.m_metaDown = ev->modifiers & cmdKey;
1481 event.m_keyCode = keyval;
1482 event.m_x = ev->where.h;
1483 event.m_y = ev->where.v;
1484 event.m_timeStamp = ev->when;
1485 event.SetEventObject(focus);
1486 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1487 if ( !handled )
1488 {
1489 #if wxUSE_ACCEL
1490 if (!handled)
1491 {
1492 /*
1493 wxWindow *ancestor = focus;
1494 while (ancestor)
1495 {
1496 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1497 if (command != -1)
1498 {
1499 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1500 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1501 break;
1502 }
1503 if (ancestor->m_isFrame)
1504 break;
1505 ancestor = ancestor->GetParent();
1506 }
1507 */
1508 }
1509 #endif // wxUSE_ACCEL
1510 }
1511 if (!handled)
1512 {
1513 wxKeyEvent event(wxEVT_CHAR);
1514 event.m_shiftDown = ev->modifiers & shiftKey;
1515 event.m_controlDown = ev->modifiers & controlKey;
1516 event.m_altDown = ev->modifiers & optionKey;
1517 event.m_metaDown = ev->modifiers & cmdKey;
1518 event.m_keyCode = keyval;
1519 event.m_x = ev->where.h;
1520 event.m_y = ev->where.v;
1521 event.m_timeStamp = ev->when;
1522 event.SetEventObject(focus);
1523 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1524 }
1525 if ( !handled &&
1526 (keyval == WXK_TAB) &&
1527 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
1528 (focus->GetParent()) &&
1529 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
1530 {
1531 wxNavigationKeyEvent new_event;
1532 new_event.SetEventObject( focus );
1533 new_event.SetDirection( !event.ShiftDown() );
1534 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1535 new_event.SetWindowChange( event.ControlDown() );
1536 new_event.SetCurrentFocus( focus );
1537 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1538 }
1539 }
1540 if ( !handled )
1541 {
1542 // if window is not having a focus still testing for default enter or cancel
1543 // TODO add the UMA version for ActiveNonFloatingWindow
1544 focus = wxFindWinFromMacWindow( FrontWindow() ) ;
1545 if ( focus )
1546 {
1547 if ( keyval == WXK_RETURN )
1548 {
1549 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
1550 wxButton);
1551 if ( def && def->IsEnabled() )
1552 {
1553 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1554 event.SetEventObject(def);
1555 def->Command(event);
1556 return ;
1557 }
1558 }
1559 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1560 else if (keyval == WXK_ESCAPE || (keyval == '.' && ev->modifiers & cmdKey ) )
1561 {
1562 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1563 new_event.SetEventObject( focus );
1564 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1565 }
1566 }
1567 }
1568 }
1569 }
1570
1571 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr )
1572 {
1573 EventRecord* ev = (EventRecord*) evr ;
1574 wxToolTip::RemoveToolTips() ;
1575
1576 UInt32 menuresult = UMAMenuEvent(ev) ;
1577 if ( HiWord( menuresult ) )
1578 {
1579 }
1580 else
1581 {
1582 short keycode ;
1583 short keychar ;
1584 keychar = short(ev->message & charCodeMask);
1585 keycode = short(ev->message & keyCodeMask) >> 8 ;
1586
1587 wxWindow* focus = wxWindow::FindFocus() ;
1588 if ( focus )
1589 {
1590 long keyval = wxMacTranslateKey(keychar, keycode) ;
1591
1592 wxKeyEvent event(wxEVT_KEY_UP);
1593 event.m_shiftDown = ev->modifiers & shiftKey;
1594 event.m_controlDown = ev->modifiers & controlKey;
1595 event.m_altDown = ev->modifiers & optionKey;
1596 event.m_metaDown = ev->modifiers & cmdKey;
1597 event.m_keyCode = keyval;
1598 event.m_x = ev->where.h;
1599 event.m_y = ev->where.v;
1600 event.m_timeStamp = ev->when;
1601 event.SetEventObject(focus);
1602 bool handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1603 }
1604 }
1605 }
1606
1607 void wxApp::MacHandleActivateEvent( WXEVENTREF evr )
1608 {
1609 EventRecord* ev = (EventRecord*) evr ;
1610 WindowRef window = (WindowRef) ev->message ;
1611 if ( window )
1612 {
1613 bool activate = (ev->modifiers & activeFlag ) ;
1614 WindowClass wclass ;
1615 ::GetWindowClass ( window , &wclass ) ;
1616 if ( wclass == kFloatingWindowClass )
1617 {
1618 // if it is a floater we activate/deactivate the front non-floating window instead
1619 window = ::FrontNonFloatingWindow() ;
1620 }
1621 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1622 if ( win )
1623 win->MacActivate( ev , activate ) ;
1624 }
1625 }
1626
1627 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr )
1628 {
1629 EventRecord* ev = (EventRecord*) evr ;
1630 WindowRef window = (WindowRef) ev->message ;
1631 wxTopLevelWindowMac * win = wxFindWinFromMacWindow( window ) ;
1632 if ( win )
1633 {
1634 if ( !wxPendingDelete.Member(win) )
1635 win->MacUpdate( ev->when ) ;
1636 }
1637 else
1638 {
1639 // since there is no way of telling this foreign window to update itself
1640 // we have to invalidate the update region otherwise we keep getting the same
1641 // event over and over again
1642 BeginUpdate( window ) ;
1643 EndUpdate( window ) ;
1644 }
1645 }
1646
1647 void wxApp::MacHandleDiskEvent( WXEVENTREF evr )
1648 {
1649 EventRecord* ev = (EventRecord*) evr ;
1650 if ( HiWord( ev->message ) != noErr )
1651 {
1652 #if !TARGET_CARBON
1653 OSErr err ;
1654 Point point ;
1655 SetPt( &point , 100 , 100 ) ;
1656
1657 err = DIBadMount( point , ev->message ) ;
1658 wxASSERT( err == noErr ) ;
1659 #endif
1660 }
1661 }
1662
1663 void wxApp::MacHandleOSEvent( WXEVENTREF evr )
1664 {
1665 EventRecord* ev = (EventRecord*) evr ;
1666 switch( ( ev->message & osEvtMessageMask ) >> 24 )
1667 {
1668 case suspendResumeMessage :
1669 {
1670 bool isResuming = ev->message & resumeFlag ;
1671 #if !TARGET_CARBON
1672 bool convertClipboard = ev->message & convertClipboardFlag ;
1673 #else
1674 bool convertClipboard = false;
1675 #endif
1676 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
1677 if ( isResuming )
1678 {
1679 WindowRef oldFrontWindow = NULL ;
1680 WindowRef newFrontWindow = NULL ;
1681
1682 // in case we don't take care of activating ourselves, we have to synchronize
1683 // our idea of the active window with the process manager's - which it already activated
1684
1685 if ( !doesActivate )
1686 oldFrontWindow = ::FrontNonFloatingWindow() ;
1687
1688 MacResume( convertClipboard ) ;
1689
1690 newFrontWindow = ::FrontNonFloatingWindow() ;
1691
1692 if ( oldFrontWindow )
1693 {
1694 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( oldFrontWindow ) ;
1695 if ( win )
1696 win->MacActivate( ev , false ) ;
1697 }
1698 if ( newFrontWindow )
1699 {
1700 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( newFrontWindow ) ;
1701 if ( win )
1702 win->MacActivate( ev , true ) ;
1703 }
1704 }
1705 else
1706 {
1707 MacSuspend( convertClipboard ) ;
1708
1709 // in case this suspending did close an active window, another one might
1710 // have surfaced -> lets deactivate that one
1711
1712 /* TODO : find out what to do on systems < 10 , perhaps FrontNonFloatingWindow
1713 WindowRef newActiveWindow = ::ActiveNonFloatingWindow() ;
1714 if ( newActiveWindow )
1715 {
1716 wxWindow* win = wxFindWinFromMacWindow( newActiveWindow ) ;
1717 if ( win )
1718 win->MacActivate( ev , false ) ;
1719 }
1720 */
1721 }
1722 }
1723 break ;
1724 case mouseMovedMessage :
1725 {
1726 WindowRef window;
1727
1728 wxWindow* currentMouseWindow = NULL ;
1729
1730 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
1731 &currentMouseWindow ) ;
1732 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
1733 {
1734 wxMouseEvent event ;
1735
1736 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
1737 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
1738
1739 event.m_leftDown = isDown && !controlDown;
1740 event.m_middleDown = FALSE;
1741 event.m_rightDown = isDown && controlDown;
1742 event.m_shiftDown = ev->modifiers & shiftKey;
1743 event.m_controlDown = ev->modifiers & controlKey;
1744 event.m_altDown = ev->modifiers & optionKey;
1745 event.m_metaDown = ev->modifiers & cmdKey;
1746 event.m_x = ev->where.h;
1747 event.m_y = ev->where.v;
1748 event.m_timeStamp = ev->when;
1749 event.SetEventObject(this);
1750
1751 if ( wxWindow::s_lastMouseWindow )
1752 {
1753 wxMouseEvent eventleave(event);
1754 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
1755 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
1756
1757 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
1758 }
1759 if ( currentMouseWindow )
1760 {
1761 wxMouseEvent evententer(event);
1762 evententer.SetEventType( wxEVT_ENTER_WINDOW );
1763 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
1764
1765 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
1766 }
1767 wxWindow::s_lastMouseWindow = currentMouseWindow ;
1768 }
1769
1770 short windowPart = ::FindWindow(ev->where, &window);
1771
1772 switch (windowPart)
1773 {
1774 // fixes for setting the cursor back from dominic mazzoni
1775 case inMenuBar :
1776 UMAShowArrowCursor();
1777 break ;
1778 case inSysWindow :
1779 UMAShowArrowCursor();
1780 break ;
1781 default:
1782 {
1783 // if ( s_lastMouseDown == 0 )
1784 // ev->modifiers |= btnState ;
1785
1786 // Calling GetNextEvent with a zero event mask will always
1787 // pass back a null event. However, it fills the EventRecord
1788 // with the state of the modifier keys. This is needed since
1789 // the modifier state returned by WaitForNextEvent often is
1790 // wrong mouse move events. The attempt above to correct this
1791 // didn't always work (under OS X at least).
1792
1793 EventRecord tmp;
1794 ::GetNextEvent(0, &tmp);
1795 ev->modifiers = tmp.modifiers;
1796
1797 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1798 if ( win )
1799 win->MacMouseMoved( ev , windowPart ) ;
1800 else
1801 UMAShowArrowCursor();
1802
1803 }
1804 break;
1805 }
1806 }
1807 break ;
1808
1809 }
1810 }
1811
1812 void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
1813 {
1814 if (macMenuId == 0)
1815 return; // no menu item selected
1816
1817 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
1818 {
1819 #if ! TARGET_CARBON
1820 Str255 deskAccessoryName ;
1821 GrafPtr savedPort ;
1822
1823 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
1824 GetPort(&savedPort);
1825 OpenDeskAcc(deskAccessoryName);
1826 SetPort(savedPort);
1827 #endif
1828 }
1829 else
1830 {
1831 wxWindow* frontwindow = wxFindWinFromMacWindow( ::FrontWindow() ) ;
1832 if ( frontwindow && wxMenuBar::MacGetInstalledMenuBar() )
1833 wxMenuBar::MacGetInstalledMenuBar()->MacMenuSelect( frontwindow->GetEventHandler() , 0 , macMenuId , macMenuItemNum ) ;
1834 }
1835 HiliteMenu(0);
1836 }
1837
1838 /*
1839 long wxApp::MacTranslateKey(char key, int mods)
1840 {
1841 }
1842
1843 void wxApp::MacAdjustCursor()
1844 {
1845 }
1846
1847 */
1848 /*
1849 void
1850 wxApp::macAdjustCursor()
1851 {
1852 if (ev->what != kHighLevelEvent)
1853 {
1854 wxWindow* theMacWxFrame = wxFrame::MacFindFrameOrDialog(::FrontWindow());
1855 if (theMacWxFrame)
1856 {
1857 if (!theMacWxFrame->MacAdjustCursor(ev->where))
1858 ::SetCursor(&(qd.arrow));
1859 }
1860 }
1861 }
1862 */